4

考慮這種可變參數模板瘋狂從一種類型轉換的陣列到另一:Variadic模板元編程:鏗鏘++或g ++中的錯誤?

#include <array> 
#include <type_traits> 

template <typename Type> 
class Converter 
{ 
    public: 
     template <typename OtherType, unsigned int OtherSize, class Array, typename... Types, class = typename std::enable_if<sizeof...(Types) != OtherSize>::type> 
     static constexpr const std::array<OtherType, OtherSize> convert(const Array source, const Types&... values); 
     template <typename OtherType, unsigned int OtherSize, class Array, typename... Types, class = typename std::enable_if<sizeof...(Types) == OtherSize>::type> 
     static constexpr const std::array<OtherType, OtherSize> convert(const Array, const Types... values); 
}; 

template <typename Type> 
template <typename OtherType, unsigned int OtherSize, class Array, typename... Types, class> 
constexpr const std::array<OtherType, OtherSize> Converter<Type>::convert(const Array source, const Types&... values) 
{ 
    return convert<OtherType, OtherSize>(source, values..., OtherType(source[sizeof...(values)])); 
} 

template <typename Type> 
template <typename OtherType, unsigned int OtherSize, class Array, typename... Types, class> 
constexpr const std::array<OtherType, OtherSize> Converter<Type>::convert(const Array, const Types... values) 
{ 
    return std::array<OtherType, OtherSize>({{values...}}); 
} 

int main(int argc, char* argv[]) 
{ 
    Converter<double>::convert<int, 3>(std::array<double, 3>({{1., 2., 3.}})); 
    return 0; 
} 

此代碼編譯以及下克++ 4.7和g ++ 4.8但不低於鐺++ 3.2:

main.cpp:16:67: error: conflicting types for 'convert' 
constexpr const std::array<OtherType, OtherSize> Converter<Type>::convert(const Array source, const Types&... values) 
                   ^
main.cpp:9:65: note: previous declaration is here 
     static constexpr const std::array<OtherType, OtherSize> convert(const Array source, const Types&... values); 
                   ^
main.cpp:23:67: error: conflicting types for 'convert' 
constexpr const std::array<OtherType, OtherSize> Converter<Type>::convert(const Array, const Types... values) 
                   ^
main.cpp:11:65: note: previous declaration is here 
     static constexpr const std::array<OtherType, OtherSize> convert(const Array, const Types... values); 

g ++是否過於寬容,或者它是鏗鏘++的錯誤(如果是這樣,是否有一個公共bugng的bug追蹤器)?

回答

4

是啊,這是this bug已經在鐺報告並固定。

+0

並且與其他一些主要編譯器供應商不同,此類錯誤修復很快就會在新版本中發佈。 – TemplateRex 2013-05-09 21:53:13