简体   繁体   中英

Variadic template metaprogramming : a bug in clang++ or g++?

Consider this variadic template madness to cast an array from one type to another:

#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;
}

This code compiles well under g++4.7 and g++4.8 but not under clang++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);

Is g++ too permissive or is it a bug in clang++ (and if so, is there a public bugtracker of clang++) ?

是的,这就是已经在clang和修复中报告的这个bug

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM