2017-08-10 73 views
0

我試圖編譯GCC 5.4以下的C++代碼14(online example):GCC5:嵌套變量模板不是函數模板?

template<typename T> 
struct traits { 
    template<typename X> 
    static constexpr bool vect = true; 
}; 

template<typename T1, typename T2> 
constexpr bool all_vect = traits<T1>::template vect<T2>; 

bool something() { 
    return all_vect<void, double>; 
} 

,但我得到了以下錯誤:

<source>: In instantiation of 'constexpr const bool all_vect<void, double>': 
<source>:11:12: required from here 
<source>:8:16: error: 'template<class X> constexpr const bool traits<void>::vect<X>' is not a function template 
constexpr bool all_vect = traits<T1>::template vect<T2>; 
       ^
<source>:8:16: error: 'vect<T2>' is not a member of 'traits<void>' 
Compiler exited with result code 1 

雖然我在GCC沒有問題6.1或更多或在3.9以上。但是我嘗試過的所有GCC5版本都是一樣的。

我不明白爲什麼?通常,GCC5應該是C++ 14功能完備的。

在GCC5中仍然使用變量模板的問題有一個簡單的解決方法嗎?我寧願不回去使用簡單的特徵,因爲我將所有特徵轉換爲使用可變模板。

回答

1

這是一個固定在gcc6中的bug,如圖中所示。

看起來好像在保持模板變量時沒有解決方法。

對於不願意用可變模板,您可以用好老靜態的非模板變量走了解決方法:

template<typename T> 
struct traits { 

    template<typename X> 
    struct Is_vect 
    { 
     static constexpr bool value = true; 
    }; 
}; 

template<typename T1, typename T2> 
struct Are_all_vect 
{ 
    static constexpr bool value = traits<T1>::template Is_vect<T2>::value; 
}; 


bool something() { 
    return Are_all_vect<void, double>::value; 
} 

或constexpr模板功能:

template<typename T> 
struct traits { 
    template<typename X> 
    static constexpr bool vect() { return true; } 
}; 

template<typename T1, typename T2> 
constexpr bool all_vect() { return traits<T1>::template vect<T2>(); } 

bool something() { 
    return all_vect<void, double>(); 
} 
+0

謝謝回答。我實際上正在尋找一個更簡單的解決方法,仍然使用變量模板。我寧願不回去使用簡單的特徵,因爲我將所有特徵轉換爲使用可變模板。 –

+1

@BaptisteWicht它看起來像你唯一的選擇是編譯器的一個新版本,修正了錯誤或回到舊的特徵,或者你可以使用constexpr函數。 – bolov