2014-01-27 18 views
5

我正在嘗試編寫代碼(使用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; } 
+3

編譯器中的分段錯誤總是編譯器中的一個錯誤 – bobah

+0

可以在gcc 4.8.2 – pmr

+0

上重現此編譯器不應該基於賦給它們的代碼崩潰......所以這絕對是一個錯誤。你可能想嘗試clang ++,看看它是否做了不同的事情。如果還沒有的話,你可能想在gcc bugzilla中引發一個bug。 –

回答

1

它肯定一個克++錯誤。但是你的代碼不正確。

template <typename R, typename ...Args> 
//using zip_t = zip<R, Args...>::with<Args...>; 
using zip_t = typename zip<R, Args...>::template with<Args...>; 

問題是,您錯過了兩個關鍵字,一個typename和一個模板。

如果添加模板或typename但錯過了其他,gcc將不會核心段錯誤。鏗鏘會比gcc好,它會告訴你,你錯過了typename關鍵字或模板關鍵字。

但是,如果你錯過了這兩個,gcc將seg故障,並且clang會告訴你一個你錯過了typename的錯誤,但它不會告訴你你錯過了模板。