您好,
我尝试用VS2017 15.7.3&编译以下代码。 / STD:C ++最新:
#include "variant"
struct OptionOne {
OptionOne() {}
OptionOne(OptionOne&& other) noexcept = default;
OptionOne(const OptionOne& other) = delete;
int X = 2;
};
struct OptionTwo {
OptionTwo() {}
OptionTwo(OptionTwo&& other) noexcept = default;
OptionTwo(const OptionTwo& other) = delete;
int X = 4;
};
using Store = std::variant;
int main()
{
Store test;
OptionTwo two;
test = std::move(two);
return 0;
}
此代码无法编译,并显示错误消息:
>c:\users\jean\source\repos\variant\variant.cpp(32): error C2280: 'std::variant &std::variant::operator =(const std::variant &)': attempting to reference a deleted function
1>c:\program files (x86)\microsoft visual studio\2017\enterprise\vc\tools\msvc\14.14.26428\include\variant(1343): note: compiler has generated 'std::variant::operator =' here
1>c:\program files (x86)\microsoft visual studio\2017\enterprise\vc\tools\msvc\14.14.26428\include\variant(1343): note: 'std::variant &std::variant::operator =(const std::variant &)': function was implicitly deleted because a base class invokes a deleted or inaccessible function 'std::_Deleted_move_assign &std::_Deleted_move_assign::operator =(const std::_Deleted_move_assign &)'
1> with
1> [
1> _Base=std::_Variant_base
1> ]
1>c:\program files (x86)\microsoft visual studio\2017\enterprise\vc\tools\msvc\14.14.26428\include\xsmf_control.h(162): note: 'std::_Deleted_move_assign &std::_Deleted_move_assign::operator =(const std::_Deleted_move_assign &)': function was implicitly deleted because a base class invokes a deleted or inaccessible function 'std::_Deleted_copy_assign &std::_Deleted_copy_assign::operator =(const std::_Deleted_copy_assign &)'
1> with
1> [
1> _Base=std::_Variant_base
1> ]
1>c:\program files (x86)\microsoft visual studio\2017\enterprise\vc\tools\msvc\14.14.26428\include\xsmf_control.h(109): note: 'std::_Deleted_copy_assign &std::_Deleted_copy_assign::operator =(const std::_Deleted_copy_assign &)': function was explicitly deleted
1> with
1> [
1> _Base=std::_Variant_base
1> ]
据我所知,这个错误信息似乎是编译器试图将(删除)复制操作符用于已删除的变体。
以我的拙见,它应该使用operator =的移动版本。
这段代码有什么问题吗?
谢谢你的帮助! 让