简体   繁体   English

为什么std :: optional :: operator =(U &&)要求U是非标量类型?

[英]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>>> is false ... 此函数不应参与重载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: 我们有一堆赋值重载 ,它们采用:

  1. nullopt_t
  2. optional const&
  3. optional&&
  4. U&&
  5. optional<U> const&
  6. 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.

相关问题 从'std :: string'转换为请求的非标量类型 - conversion from 'std::string' to non-scalar type requested 为什么 operator==(std::variant<T, U> , T) 不行吗? - Why does operator==(std::variant<T, U>, T) not work? 请求的非标量类型 - non-scalar type requested 从&#39;std :: vector转换 <AdjacencyData> (*)()&#39;为非标量类型&#39;std :: vector <AdjacencyData> 要求 - conversion from ‘std::vector<AdjacencyData> (*)()’ to non-scalar type ‘std::vector<AdjacencyData>’ requested 错误:转换为非标量类型 - Error: Conversion to non-scalar type 我该如何解决错误:从&#39;void&#39;转换为非标量类型&#39;std :: vector <int> 要求 - how can I fix the error : conversion from ‘void’ to non-scalar type ‘std::vector<int>’ requested 从请求到非标量类型的转换 - conversion from to non-scalar type requested c ++ struct运算符:要求从转换为非标量类型 - c++ struct operator: conversion from to non-scalar type requested 从&#39;int&#39;转换为非标量类型赋值运算符-将对象转换为int - Conversion from 'int' to non-scalar type assignment operator - object to int 此错误的含义是什么:从 &#39;char* [40] 转换为非标量类型 &#39;std::string - What it is the meaning of this error: conversion from 'char* [40] to non-scalar type 'std::string
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM