小编Jor*_*dan的帖子

为什么这会超过最大递归模板深度?

我一直在使用可变参数模板并注意到以下内容.

这很好用:

auto t = std::make_tuple(1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16);
Run Code Online (Sandbox Code Playgroud)

这将给出错误(gcc 4.8.2(编辑:Clang 3.4)默认最大深度为256):

auto t2 = std::make_tuple(1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17);
Run Code Online (Sandbox Code Playgroud)

但是,直接创建元组将起作用:

std::tuple<int,int,int,int,int,int,int,int,int,int,int,int,int,int,int,int,int> t3(1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17);
Run Code Online (Sandbox Code Playgroud)

我在尝试创建一个返回模板化类的模板化函数时注意到了这一点.

template <typename...Arguments>
struct Testing {
  std::tuple<Arguments...> t;
  Testing(Arguments...args) : t(args...) {}
};

template <typename... Arguments>
Testing<Arguments...> create(Arguments... args) {
  return Testing<Arguments...>(args...);
}
Run Code Online (Sandbox Code Playgroud)

在这种情况下,这将工作:

auto t4 = create(1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16);
Run Code Online (Sandbox Code Playgroud)

这不会:

auto t5 = create(1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17);
Run Code Online (Sandbox Code Playgroud)

c++ templates variadic-templates c++11

22
推荐指数
1
解决办法
1995
查看次数

标签 统计

c++ ×1

c++11 ×1

templates ×1

variadic-templates ×1