【发布时间】:2017-01-22 19:56:11
【问题描述】:
给定一个代码:
constexpr auto a = std::make_array<size_t>(1, 2, 3);
具有从 GCC 的 libstdc++v3 experimental/array 复制的实现的 Clang (3.7) 给出以下警告:
error: non-constant-expression cannot be narrowed from type 'int' to 'value_type' (aka 'unsigned long') in initializer list [-Wc++11-narrowing]
return {{ std::forward<Types>(t)... }};
当编译器在编译时知道 1、2 和 3 可以隐式转换为 size_t 时,这是否合法?
我写的时候没有警告:
constexpr std::array<size_t, 3> a{1, 2, 3};
而std::make_array应该和这个构造一样。
这是一个比实际问题更理论的问题。
额外问题:如何在 GCC 的实现中纠正 std::make_array 以接受上面给出的代码?
GCC的实现:
template <typename _Dest = void, typename... _Types>
constexpr auto
make_array(_Types&&... __t)
-> array<conditional_t<is_void_v<_Dest>,
common_type_t<_Types...>,
_Dest>,
sizeof...(_Types)>
{
static_assert(__or_<
__not_<is_void<_Dest>>,
__and_<__not_<__is_reference_wrapper<decay_t<_Types>>>...>>
::value,
"make_array cannot be used without an explicit target type "
"if any of the types given is a reference_wrapper");
return {{forward<_Types>(__t)...}};
}
【问题讨论】:
标签: c++ arrays language-lawyer c++17