我寫了一個類似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) –
這是真正的代碼。我以爲我犯了你描述的錯誤,但事實上我沒有。 –