【发布时间】:2013-05-23 01:39:21
【问题描述】:
我在写C++11std::tuple-like 类并尝试用g++-4.7 编译它时遇到了一个非常奇怪的情况。我基本上需要的是一个包装类型的元组。我写了这样的东西:
#include <tuple>
template <class T>
struct Wrapper { T x; };
template <class... Types>
using Tuple = std::tuple<Wrapper<Types>...>;
template <class... Types>
struct X
{
using MyTuple = Tuple<Types...>;
};
int main( int argc, char** argv )
{
// Tuple<int,int> t; // (1)
using Y = X<int,int>;
Y y; // (2)
return 0;
}
我做了以下观察:
- 代码无法编译:
- 如果我添加
(1),它会编译。 - 如果我删除
(1)和(2),它也会编译。
1. 的错误消息:
test.cpp: In instantiation of ‘struct X<int, int>’:
test.cpp:22:4: required from here
test.cpp:10:44: error: wrong number of template arguments (2, should be 1)
test.cpp:4:8: error: provided for ‘template<class T> struct Wrapper’
问题:我认为上面的代码是正确的,但这是我第一次真正使用参数包。 g++-4.7 不喜欢我的代码,除了它是一个实验性实现之外,还有什么原因吗?
【问题讨论】:
-
您提供的代码编译得很好,从错误来看,您似乎不小心写了
std::tuple<Wrapper<Types...>>而不是std::tuple<Wrapper<Types>...>。请提供真实代码并在发布前重试。 -
这是真正的代码。我以为我犯了你描述的错误,但实际上我没有。
标签: c++ c++11 g++ tuples variadic-templates