Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/templates/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Templates c++;11模板循环中的decltype和无限递归_Templates_C++11_Recursion_Typetraits_Decltype - Fatal编程技术网

Templates c++;11模板循环中的decltype和无限递归

Templates c++;11模板循环中的decltype和无限递归,templates,c++11,recursion,typetraits,decltype,Templates,C++11,Recursion,Typetraits,Decltype,考虑以下代码: #包括 #包括 使用名称空间std; 结构A{ char*ka; }; 结构B{ char*baa; }; 模板 构造我的\u对{ 我的配对(t0el0,t1el1):el0(el0),el1(el1){} T0-el0; T1 el1;//可能是下一对 }; 模板 自动constexpr get_my(const T&T,typename std::enable_if::type*=0)->decltype(T.el0) { 返回t.el0; } 模板 自动constexpr

考虑以下代码:

#包括
#包括
使用名称空间std;
结构A{
char*ka;
};
结构B{
char*baa;
};
模板
构造我的\u对{
我的配对(t0el0,t1el1):el0(el0),el1(el1){}
T0-el0;
T1 el1;//可能是下一对
};
模板
自动constexpr get_my(const T&T,typename std::enable_if::type*=0)->decltype(T.el0)
{
返回t.el0;
}
模板
自动constexpr get_my(const T&T,typename std::enable_if::type*=0)->
decltype(获取我的(t.el1))
{
返回get_my(t.el1);
}
int main()
{
my_对p1(12.789,{B(),3.14});
自动el1=获取我的(p1);
int t=1;
}
这里,我试图得到“数组”的第n个元素(类似于boost fusion的可变序列)
当我编译它时,编译器说:

error: template instantiation depth exceeds maximum of 900 (use -ftemplate-depth= to increase the maximum) substituting 'template<class T, int i, int current> constexpr decltype (get_my<T, i, (current + 1)>(t.el1)) get_my(const T&, typename std::enable_if<(i != current)>::type*) [with T = my_pair<int, my_pair<B, double> >; int i = 1; int current = 900]'
     decltype( get_my<T, i, current+1>(t.el1) )
错误:模板实例化深度超过最大值900(使用-ftemplate depth=增加最大值)替换为“模板constexpr decltype(get_my(t.el1))get_my(const t&,typename std::enable_if::type*)[带t=my_对;int i=1;int current=900]”
decltype(获取我的(t.el1))

当i==current时,为什么不调用重载get_my

将导出的
模板
参数放在最后。如果您还需要有时指定的默认参数,请考虑转发不同的超载。

template<typename T, int i, int current = 0>
auto constexpr get_my(const T& t, typename std::enable_if< i!=current >::type* = 0) ->
  decltype( get_my<T, i, current+1>(t.el1) )
{
  return get_my<T, i, current+1>(t.el1);
}
template<int i, typename T>
auto constexpr get_my // implementation with current=0
template<int i, int current, typename T>
auto constexpr get_my
模板
auto constexpr get_my//当前值为0的实现
模板
自动constexpr获取我的
不要再明确地提到它们,代码就会变得更简单,bug就会消失

(您明确使用了错误的类型)


我不明白。请你再解释一下好吗?你的意思是,这允许删除这个decltype(t.el1)以换取get_my(t.el1)@是的。删除第一个类型参数,无论在何处,并让它被推断。
模板T
:这里有一个错误。非默认模板参数如何跟在默认模板参数后面?@Constructor修复了这个错误。实时示例演示了它的编译。在您的实时示例中有
typename T
。默认模板参数后面跟着非默认模板参数呢?
template<typename T, int i, int current = 0>
auto constexpr get_my(const T& t, typename std::enable_if< i!=current >::type* = 0) ->
  decltype( get_my<decltype(t.el1), i, current+1>(t.el1) )
{
  return get_my<decltype(t.el1), i, current+1>(t.el1);
}
template<int i, typename T>
auto constexpr get_my // implementation with current=0
template<int i, int current, typename T>
auto constexpr get_my