2013-01-15 43 views
13

對於以下代碼靜態constexpr成員函數沒有發現

#include <array> 

template<unsigned MaxP, typename type> 
struct kernel 
{ 
    static constexpr unsigned max_pole(unsigned P) 
    { return P>MaxP? MaxP:P; } 

    template<unsigned P> 
    using array = std::array<type,max_pole(P)>;   // wrong? 

    template<unsigned P> 
    static void do_something(array<P> const&, array<P>&); 
}; 

GCC 4.7.0(克++ -c -std = C++ 11)給出

error: ‘max_pole’ was not declared in this scope 

這是正確(編譯器的行爲)?請注意,如果我通過將kernel::max_pole替換爲kernel::max_pole來解決max_pole,那麼它編譯的很好。

編輯向bugzilla報告,接受爲bug C++/55992,請參閱http://gcc.gnu.org/bugzilla/show_bug.cgi?id=55992。 gcc 4.7.x和4.8.0也會出現這種情況。

+0

只是測試:同爲'G ++ 4.7.2'有人有像4.8新版本?也許這是一個已修復的錯誤... – leemes

回答

9

你的模板編譯罰款鏗鏘3.2。我堅信它是一個GCC bug(它也存在於GCC 4.7.2中,順便說一下)。更改GCC 4.8.0的註釋似乎沒有提及任何此類錯誤修正。

另請注意,如果您刪除的do_something<>聲明,不應該有任何區別編譯錯誤消失。

還有一個提示:雖然此模板不彙編GCC 4.7.2:

template<unsigned MaxP, typename type> 
struct kernel 
{ 
    static constexpr unsigned max_pole(unsigned P) 
    { return P>MaxP? MaxP:P; } 

    template<typename T> 
    using array2 = int[max_pole(3)]; // ERROR! 

    static void do_something(array2<int> const&, array2<int>&); 
}; 

此模板確實編譯:

template<unsigned MaxP, typename type> 
struct kernel 
{ 
    static constexpr unsigned max_pole(unsigned P) 
    { return P>MaxP? MaxP:P; } 

    // template<typename T> <--- removed 
    using array2 = int[max_pole(3)]; // OK 

    static void do_something(array2 const&, array2&); 
}; 

由於max_pole是在兩種情況下不合格的獨立名稱,查找策略爲d在兩種情況下都是相同的,但事實並非如此。對我來說,這是一個錯誤。

+0

因此,根據您的研究,這種情況會在某些情況下混合兩個新的C++ 11功能('constexpr' +別名模板)時發生。那麼編譯器錯誤就是完美的場景。 – Gorpik

+0

@Gorpik:是的,我有同感 –

+1

+1謝謝。我有同樣的感覺。在bugzilla報道。 – Walter