[英]C++ parameter pack fails to expand
我正在使用可变参数模板,我无法理解为什么以下代码无法编译(GCC 4.9.2 with std = c ++ 11):
这只是一个例子,但我需要在我的代码中使用类似的方法,它也会失败:
template<int I>
class Type{};
template<typename ... Tlist>
class A{
public:
template<int ...N>
void a(Type<N> ... plist){
}
};
template<typename ... Tlist>
class B{
public:
template<int ... N>
void b(Type<N> ... plist){
A<Tlist...> a;
a.a<N...>(plist ...);
}
};
并使用示例:
B<int, int, int> b;
b.b<1,7,6>(Type<1>(),Type<7>(),Type<6>());
我收到以下错误:
file.cpp: In member function ‘void B<Tlist>::b(Type<N>...)’:
file.cpp:58:9: error: expected ‘;’ before ‘...’ token
a.a<N...>(plist ...);
^
file.cpp:58:24: error: parameter packs not expanded with ‘...’:
a.a<N...>(plist ...);
^
file.cpp:58:24: note: ‘N’
但是下面的代码编译得很好(我刚从两个类中删除了Tlist参数并相应地调整了代码):
template<int I>
class Type{};
class A{
public:
template<int ...N>
void a(Type<N> ... plist){
}
};
class B{
public:
template<int ... N>
void b(Type<N> ... plist){
A a;
a.a<N...>(plist ...);
}
};
B b;
b.b(Type<1>(),Type<7>(),Type<6>());
任何人都可以给我一个解释吗? 谢谢。
编译器没有理由相信aa
是一个模板; 因此,它被强制解释<
作为小于运算符。 写:
a.template a<N...>(plist ...);
^^^^^^^^^
在你的第二个例子,它知道aa
是一个模板,因为类型a
有不依赖于模板参数。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.