当 T 包含一个 const 数据成员时,为什么要删除 std::optional<T>::operator=?
Why is `std::optional<T>::operator=` deleted when T contains a `const` data member?
以下代码会导致编译错误:
#include <optional>
class A {
};
class B {
private:
const A a;
};
int main()
{
B b;
std::optional<B> bo1;
bo1 = b;
}
例如,在 gcc 上,错误显示为:
main.cpp: In function 'int main()':
main.cpp:12:7: error: uninitialized const member in 'class B'
12 | B b;
| ^
main.cpp:7:13: note: 'const A B::a' should be initialized
7 | const A a;
| ^
main.cpp:14:11: error: use of deleted function 'std::optional<B>& std::optional<B>::operator=(std::optional<B>&&)'
14 | bo1 = b;
| ^
In file included from main.cpp:1:
/lib/gcc-head/include/c++/12.0.0/optional:663:11: note: 'std::optional<B>& std::optional<B>::operator=(std::optional<B>&&)' is implicitly deleted because the default definition would be ill-formed:
663 | class optional
| ^~~~~~~~
/lib/gcc-head/include/c++/12.0.0/optional:663:11: error: use of deleted function 'std::_Enable_copy_move<true, false, true, false, _Tag>& std::_Enable_copy_move<true, false, true, false, _Tag>::operator=(std::_Enable_copy_move<true, false, true, false, _Tag>&&) [with _Tag = std::optional<B>]'
In file included from /lib/gcc-head/include/c++/12.0.0/optional:43,
from main.cpp:1:
/lib/gcc-head/include/c++/12.0.0/bits/enable_special_members.h:248:5: note: declared here
248 | operator=(_Enable_copy_move&&) noexcept = delete;
| ^~~~~~~~
在 MSVC 上,作为另一个示例,错误显示为:
main.cpp
<source>(14): error C2280: 'std::optional<B> &std::optional<B>::operator =(const std::optional<B> &)': attempting to reference a deleted function
C:/data/msvc/14.29.29917-Pre/include\optional(445): note: compiler has generated 'std::optional<B>::operator =' here
C:/data/msvc/14.29.29917-Pre/include\optional(445): note: 'std::optional<B> &std::optional<B>::operator =(const std::optional<B> &)': function was implicitly deleted because a base class invokes a deleted or inaccessible function 'std::_Deleted_move_assign<std::_Optional_construct_base<_Ty>,_Ty> &std::_Deleted_move_assign<std::_Optional_construct_base<_Ty>,_Ty>::operator =(const std::_Deleted_move_assign<std::_Optional_construct_base<_Ty>,_Ty> &)'
with
[
_Ty=B
]
C:/data/msvc/14.29.29917-Pre/include\xsmf_control.h(131): note: 'std::_Deleted_move_assign<std::_Optional_construct_base<_Ty>,_Ty> &std::_Deleted_move_assign<std::_Optional_construct_base<_Ty>,_Ty>::operator =(const std::_Deleted_move_assign<std::_Optional_construct_base<_Ty>,_Ty> &)': function was implicitly deleted because a base class invokes a deleted or inaccessible function 'std::_Deleted_copy_assign<std::_Optional_construct_base<_Ty>,_Ty> &std::_Deleted_copy_assign<std::_Optional_construct_base<_Ty>,_Ty>::operator =(const std::_Deleted_copy_assign<std::_Optional_construct_base<_Ty>,_Ty> &)'
with
[
_Ty=B
]
C:/data/msvc/14.29.29917-Pre/include\xsmf_control.h(92): note: 'std::_Deleted_copy_assign<std::_Optional_construct_base<_Ty>,_Ty> &std::_Deleted_copy_assign<std::_Optional_construct_base<_Ty>,_Ty>::operator =(const std::_Deleted_copy_assign<std::_Optional_construct_base<_Ty>,_Ty> &)': function was explicitly deleted
with
[
_Ty=B
]
Compiler returned: 2
只要我们去掉const A a;
中的const
关键字,错误就消失了。
根据 referenced standard on std::optional,根据 std::is_...able_v 测试,有些情况下 = 运算符的某些重载将被删除。在这种情况下,为什么 const
关键字会影响其中一些测试?
const 数据成员在构造对象后不能修改。因此,正常工作 operator=
是不可能的。
optional
使用对象的 =
.
无法分配给具有 const
数据成员的 class。只能建造。
试试这个:
B b0;
B b1;
b0=b1;
optional
不起作用,因为 B
不起作用。
此外,尝试 static_assert(!std::is_copy_assignable_v<B>);
,它通过了。
现在,std::optional
这里有个“后门”。
B b;
std::optional<B> bo1;
bo1.emplace(b);
构造而不是赋值。
从理论上讲,可选的可以依赖于此;但如果内容存在,则需要销毁它然后放置一个新对象。这是有风险的,会扰乱语义,而且是一个默默地做的糟糕计划。
以下代码会导致编译错误:
#include <optional>
class A {
};
class B {
private:
const A a;
};
int main()
{
B b;
std::optional<B> bo1;
bo1 = b;
}
例如,在 gcc 上,错误显示为:
main.cpp: In function 'int main()':
main.cpp:12:7: error: uninitialized const member in 'class B'
12 | B b;
| ^
main.cpp:7:13: note: 'const A B::a' should be initialized
7 | const A a;
| ^
main.cpp:14:11: error: use of deleted function 'std::optional<B>& std::optional<B>::operator=(std::optional<B>&&)'
14 | bo1 = b;
| ^
In file included from main.cpp:1:
/lib/gcc-head/include/c++/12.0.0/optional:663:11: note: 'std::optional<B>& std::optional<B>::operator=(std::optional<B>&&)' is implicitly deleted because the default definition would be ill-formed:
663 | class optional
| ^~~~~~~~
/lib/gcc-head/include/c++/12.0.0/optional:663:11: error: use of deleted function 'std::_Enable_copy_move<true, false, true, false, _Tag>& std::_Enable_copy_move<true, false, true, false, _Tag>::operator=(std::_Enable_copy_move<true, false, true, false, _Tag>&&) [with _Tag = std::optional<B>]'
In file included from /lib/gcc-head/include/c++/12.0.0/optional:43,
from main.cpp:1:
/lib/gcc-head/include/c++/12.0.0/bits/enable_special_members.h:248:5: note: declared here
248 | operator=(_Enable_copy_move&&) noexcept = delete;
| ^~~~~~~~
在 MSVC 上,作为另一个示例,错误显示为:
main.cpp
<source>(14): error C2280: 'std::optional<B> &std::optional<B>::operator =(const std::optional<B> &)': attempting to reference a deleted function
C:/data/msvc/14.29.29917-Pre/include\optional(445): note: compiler has generated 'std::optional<B>::operator =' here
C:/data/msvc/14.29.29917-Pre/include\optional(445): note: 'std::optional<B> &std::optional<B>::operator =(const std::optional<B> &)': function was implicitly deleted because a base class invokes a deleted or inaccessible function 'std::_Deleted_move_assign<std::_Optional_construct_base<_Ty>,_Ty> &std::_Deleted_move_assign<std::_Optional_construct_base<_Ty>,_Ty>::operator =(const std::_Deleted_move_assign<std::_Optional_construct_base<_Ty>,_Ty> &)'
with
[
_Ty=B
]
C:/data/msvc/14.29.29917-Pre/include\xsmf_control.h(131): note: 'std::_Deleted_move_assign<std::_Optional_construct_base<_Ty>,_Ty> &std::_Deleted_move_assign<std::_Optional_construct_base<_Ty>,_Ty>::operator =(const std::_Deleted_move_assign<std::_Optional_construct_base<_Ty>,_Ty> &)': function was implicitly deleted because a base class invokes a deleted or inaccessible function 'std::_Deleted_copy_assign<std::_Optional_construct_base<_Ty>,_Ty> &std::_Deleted_copy_assign<std::_Optional_construct_base<_Ty>,_Ty>::operator =(const std::_Deleted_copy_assign<std::_Optional_construct_base<_Ty>,_Ty> &)'
with
[
_Ty=B
]
C:/data/msvc/14.29.29917-Pre/include\xsmf_control.h(92): note: 'std::_Deleted_copy_assign<std::_Optional_construct_base<_Ty>,_Ty> &std::_Deleted_copy_assign<std::_Optional_construct_base<_Ty>,_Ty>::operator =(const std::_Deleted_copy_assign<std::_Optional_construct_base<_Ty>,_Ty> &)': function was explicitly deleted
with
[
_Ty=B
]
Compiler returned: 2
只要我们去掉const A a;
中的const
关键字,错误就消失了。
根据 referenced standard on std::optional,根据 std::is_...able_v 测试,有些情况下 = 运算符的某些重载将被删除。在这种情况下,为什么 const
关键字会影响其中一些测试?
const 数据成员在构造对象后不能修改。因此,正常工作 operator=
是不可能的。
optional
使用对象的 =
.
无法分配给具有 const
数据成员的 class。只能建造。
试试这个:
B b0;
B b1;
b0=b1;
optional
不起作用,因为 B
不起作用。
此外,尝试 static_assert(!std::is_copy_assignable_v<B>);
,它通过了。
现在,std::optional
这里有个“后门”。
B b;
std::optional<B> bo1;
bo1.emplace(b);
构造而不是赋值。
从理论上讲,可选的可以依赖于此;但如果内容存在,则需要销毁它然后放置一个新对象。这是有风险的,会扰乱语义,而且是一个默默地做的糟糕计划。