【发布时间】:2014-06-15 23:57:48
【问题描述】:
我一直在使用可变参数模板并注意到以下内容。
这很好用:
auto t = std::make_tuple(1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16);
这将给出错误(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);
但是,直接创建元组是可行的:
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);
我在尝试创建返回模板类的模板函数时注意到了这一点。
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...);
}
在这种情况下,这将起作用:
auto t4 = create(1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16);
这不会:
auto t5 = create(1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17);
【问题讨论】:
-
可能是因为
create是一个“递归模板”,当你给它17个参数时它需要17个深度,当你给它16个参数时它需要16个深度,并且限制是(显然)16。 -
g++4.8.2 需要一个正好为 231 的实例化深度来编译带有 17 个参数的
make_tuple。 Live examplemake_tuple本身只增加了一个实例化深度,据我所知,问题可能是tuple本身。 -
噢噢噢,这解释了我的困惑
-
他们绝对应该努力让这个模板超魔的一些递归性降低
-
@user3586046
noexcept检查用于移动构造函数,所以trying to move that tuple fails,甚至for much fewer arguments
标签: c++ templates c++11 variadic-templates