gpt4 book ai didi

c++ - Variadic 模板元编程 : a bug in clang++ or g++?

转载 作者:塔克拉玛干 更新时间:2023-11-03 01:54:55 24 4
gpt4 key购买 nike

考虑这种将数组从一种类型转换为另一种类型的疯狂的可变参数模板:

#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++ 的公共(public)错误跟踪器)?

最佳答案

是的,就是this bug已在 clang 中报告并已修复。

关于c++ - Variadic 模板元编程 : a bug in clang++ or g++?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16469196/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com