我正在嘗試編寫代碼(使用mingw32 gcc4.8.1截至2014年1月),涉及兩個參數包Args1...
和Args2...
。我在這裏學到了(http://en.cppreference.com/w/cpp/language/parameter_pack),我需要這樣的嵌套類。 Args1...
和Args2...
是相同的長度(並且最終我希望從Args1...
推導出Args2...
)。有時甚至可以是同一類型。所以我想寫一個速記using
語句這個簡單例子作爲開始:編譯該語句時gcc segfault編譯嵌套參數包代碼
template <typename R, typename ...Args>
using zip_t = zip<R, Args...>::with<Args...>;
然而,GCC生成段故障。任何人都可以請解釋,如果我做錯了什麼或這是一個海灣合作委員會的錯誤?
g++ -std=c++11 -I.. testUsing.cpp
testUsing.cpp: In substitution of 'template<class R, class ... Args> using zip_t = zip<R, Args1 ...>::with<Args ...> [with R = A; Args = {}]':
testUsing.cpp:19:15: required from here
testUsing.cpp:16:45: internal compiler error: Segmentation fault
using zip_t = zip<R, Args...>::with<Args...>;
^
最小代碼如下:(改編自http://en.cppreference.com/w/cpp/language/parameter_pack)
template<typename...> struct Tuple {};
template<typename T1, typename T2> struct Pair {};
template<class R,class ...Args1> struct zip {
template<class ...Args2> struct with {
R flag;
typedef Tuple<Pair<Args1, Args2>...> type;
//Pair<Args1, Args2>... is the pack expansion, Pair<Args1, Args2> is the pattern
};
};
typedef zip<bool,short, int>::with<unsigned short, unsigned>::type T1;
T1 make_zip1(bool) {return T1();}
template <typename R, typename ...Args>
using zip_t = zip<R, Args...>::with<Args...>;
template<typename A>
static zip_t<A>::type make_zip(A) {
return zip_t<A>::type{};
}
//test code
int main() { return 0; }
編譯器中的分段錯誤總是編譯器中的一個錯誤 – bobah
可以在gcc 4.8.2 – pmr
上重現此編譯器不應該基於賦給它們的代碼崩潰......所以這絕對是一個錯誤。你可能想嘗試clang ++,看看它是否做了不同的事情。如果還沒有的話,你可能想在gcc bugzilla中引發一個bug。 –