gpt4 book ai didi

C++ 变体 : Why does Converting constructor require sizeof. ..(类型)为非零

转载 作者:可可西里 更新时间:2023-11-01 18:35:22 26 4
gpt4 key购买 nike

这个问题是关于:template<class...Types> class variant :

根据 variant.variant/3 ,一个实例化 variant 的程序没有模板参数的格式不正确。

到目前为止,很清楚。现在我有一个关于转换构造函数 ( template<class T> constexpr variant(T&& t) noexcept(see below) ) 的问题:

variant.variant/variant.ctor-16.1表示转换构造函数不应参与重载决策,除非:

sizeof...(Types) is nonzero

(……还有其他一些我暂时不关心的要求)。

我的问题是,当一个 variant没有模板参数已经使我的程序格式错误,为什么还要关心我的转换构造函数是否参与重载决议?

查看 variant 的 MSVC 和 libstdc++ 实现他们实际上有一个enable_if_t<sizeof...(_Types) != 0>在转换构造函数的声明中。为什么?

最佳答案

子句“sizeof...(Types) is nonzero” 已添加到 [variant.ctor] 作为论文的一部分:Some improvements to class template argument deduction integration into the standard library允许 variant也支持。

相关摘录:

Enable variant support

The following code fails to compile

variant<int, double> v1(3);
variant v2 = v1; // Ill-formed! <--THIS

As this natural code is useful and its failure is confusing, we propose that it be supported. Indeed, prior to the adoption of p0510r0 banning variant<>, the above code worked as expected since variant<> occurs in some deduction guides in the overload set. As it is not clear that constructor template argument deduction was considered in adopting p0510r0, we would like to consider allowing variant<> not to produce a hard error in such cases.

Wording (Emphasis added)
Change §23.7.3.1p16 [variant.ctor] as follows:
Remarks: This function shall not participate in overload resolution unless sizeof...(Types) is nonzero, unless is_same_v<decay_t<T>, variant> is false, unless decay_t<T> is neither a specialization of in_place_type_t nor a specialization of in_place_index_t, unless is_constructible_v<Tj, T> is true, and unless the expression FUN(std::forward(t)) (with FUN being the above-mentioned set of imaginary functions) is well formed.

所以 std::variant v2 = v1;在不考虑添加的子句的编译器版本中失败(如 GCC 7.1。请参阅 DEMO )但在更高版本(从 GCC 7.2 开始。请参阅 DEMO )上成功。

关于C++ 变体 : Why does Converting constructor require sizeof. ..(类型)为非零,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54523909/

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