[英]Why does std::optional::operator=(U&&) require U to be a non-scalar type?
For optional's template<class U = T> optional<T>& operator=(U&& v);
对于可选的template<class U = T> optional<T>& operator=(U&& v);
the standard demands that (see [optional.assign]/3.16 ): 标准要求(见[optional.assign] /3.16 ):
This function shall not participate in overload resolution unless ...
conjunction_v<is_scalar<T>, is_same<T, decay_t<U>>>
isfalse
... 此函数不应参与重载conjunction_v<is_scalar<T>, is_same<T, decay_t<U>>>
除非......conjunction_v<is_scalar<T>, is_same<T, decay_t<U>>>
为false
...
Why do we have to exclude case when assigning a scalar of type U == T
? 为什么在分配U == T
类型的标量时我们必须排除大小写?
This exists to support: 这是为了支持:
optional<int> o(42);
o = {}; // <== we want this to reset o
We have a bunch of assignment overloads , which take: 我们有一堆赋值重载 ,它们采用:
nullopt_t
optional const&
optional&&
U&&
optional<U> const&
optional<U>&&
For scalars, specifically, #4 would be a standard conversion whereas anything else would be a user-defined conversion - so it would be the best match. 对于标量,具体而言,#4将是标准转换,而其他任何东西都是用户定义的转换 - 因此它将是最佳匹配。 However, the result of that would be assigning o
to be engaged with a value of 0
. 但是,结果是指定o
使用值0
。 That would mean that o = {}
could potentially mean different things depending on the type of T
. 这意味着o = {}
可能意味着不同的东西,这取决于T
的类型。 Hence, we exclude scalars. 因此,我们排除了标量。
For non-scalars, #4 and #3 would be equivalent (both user-defined conversions), and #3 would win by being a non-template. 对于非标量,#4和#3将是等效的(两者都是用户定义的转换),#3将通过非模板获胜。 No problem there. 没问题。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.