template <class D, class...>
struct return_type_helper
{
using type = D;
};
template <class... Types>
struct return_type_helper<void, Types...> : std::common_type<Types...>
{
static_assert(
// why can't I use reference wrappers?
std::conjunction_v<not_ref_wrapper<Types>...>,
"Types cannot contain reference_wrappers when D is void"
);
};
template <class D = void, class... Types>
constexpr std::array<typename return_type_helper<D, Types...>::type, sizeof...(Types)> make_array(Types&&... t)
{
return {std::forward<Types>(t)...};
}
void foo()
{
int x = 7355608;
auto arr = make_array(std::ref(x)); // does not compile
}
为什么在使用自动类型推导时,
std::experimental::make_array()
会添加一个static_assert()
来禁止使用std::reference_wrapper
创建数组?创建引用包装器数组在其他情况下是完全合法的,编译器没有问题。auto arr2 = std::array<decltype(std::ref(x)),1>{std::ref(x)};
std::array
添加了一个扣除指南并放弃了make_array
的想法,也许深入研究这个问题并不值得? - Ted Lyngmoarr
的类型,可以发现它构造了一个合法的std::reference_wrapper
数组:struct std::array<std::reference_wrapper<int>, 1>
。 - Jacob Faibstatic_assert()
——但是我对移除检查持谨慎态度,因为我看不出它存在的明显原因... - Jacob Faib