繁体   English   中英

可变参数模板元编程:clang ++或g ++中的错误?

[英]Variadic template metaprogramming : a bug in clang++ or 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;
}

这段代码在g ++ 4.7和g ++ 4.8下编译得很好,但在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);

g ++是否过于宽松,或者它是clang ++中的错误(如果是这样,是否存在clang ++的公共错误跟踪器)?

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

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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