我写了一个类似C++11
std::tuple
的类,并试图用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>'而不是'std :: tuple ...>'。请提供真实的代码并在发布之前再次尝试。 –
Xeo
[编译于GCC 4.8。](http://coliru.stacked-crooked.com/view?id=2250fa21d618a80511a96c95725346f5-50d9cfc8a1d350e7409e81e87c2653ba) –
这是真正的代码。我以为我犯了你描述的错误,但事实上我没有。 –