考虑一下这种将数组从一种类型转换为另一种类型的疯狂模板:
#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++ 错误跟踪器)?