假设我们有一个简单的类,其中包含std::string
:
class StringWrapper {
public:
const std::string s;
StringWrapper(const std::string s)
: s(s) {}
// We want this to be moveable but not copyable
~StringWrapper() = default;
StringWrapper(const StringWrapper&) = delete;
StringWrapper& operator=(const StringWrapper&) = delete;
StringWrapper(StringWrapper&&) noexcept = default;
StringWrapper& operator=(StringWrapper&&) = default;
};
在这里,我们试图使类可移动,但不可复制。我明白,一个const
成员变量通常会阻止生成默认的移动向量,但在这里我们尝试显式生成它 - 这至少可以编译。
现在我们尝试排序std::vector<StringWrapper>
(比较函数在这里并不重要):
std::vector<StringWrapper> strings;
std::sort(strings.begin(), strings.end(), [](auto const& a, auto const& b) { return true; });
这无法编译,但错误非常神秘:
1>...\include\algorithm(7419,25): error C2280: 'StringWrapper &StringWrapper::operator =(const StringWrapper &)': attempting to reference a deleted function
1>...src\example.cpp(476): message : see declaration of 'StringWrapper::operator ='
1>...src\example.cpp(476,24): message : 'StringWrapper &StringWrapper::operator =(const StringWrapper &)': function was explicitly deleted
1>...\include\algorithm(7541): message : see reference to function template instantiation '_BidIt std::_Insertion_sort_unchecked<_RanIt,_Pr>(const _BidIt,const _BidIt,_Pr)' being compiled
1> with
1> [
1> _BidIt=StringWrapper *,
1> _RanIt=StringWrapper *,
1> _Pr=myFunc::<lambda_8668e50965d967f7b587b72f59fcb0cf>
1> ]
1>...\include\algorithm(7571): message : see reference to function template instantiation 'void std::_Sort_unchecked<StringWrapper*,_Fn>(_RanIt,_RanIt,int,_Pr)' being compiled
1> with
1> [
1> _Fn=myFunc::<lambda_8668e50965d967f7b587b72f59fcb0cf>,
1> _RanIt=StringWrapper *,
1> _Pr=myFunc::<lambda_8668e50965d967f7b587b72f59fcb0cf>
1> ]
1>...src\example.cpp(488): message : see reference to function template instantiation 'void std::sort<std::_Vector_iterator<std::_Vector_val<std::_Simple_types<_Ty>>>,myFunc::<lambda_8668e50965d967f7b587b72f59fcb0cf>>(const _RanIt,const _RanIt,_Pr)' being compiled
1> with
1> [
1> _Ty=StringWrapper,
1> _RanIt=std::_Vector_iterator<std::_Vector_val<std::_Simple_types<StringWrapper>>>,
1> _Pr=myFunc::<lambda_8668e50965d967f7b587b72f59fcb0cf>
1> ]
1>...\include\algorithm(7422,28): error C2280: 'StringWrapper &StringWrapper::operator =(const StringWrapper &)': attempting to reference a deleted function
1>...src\example.cpp(476): message : see declaration of 'StringWrapper::operator ='
1>...src\example.cpp(476,24): message : 'StringWrapper &StringWrapper::operator =(const StringWrapper &)': function was explicitly deleted
1>...\include\algorithm(7425,24): error C2280: 'StringWrapper &StringWrapper::operator =(const StringWrapper &)': attempting to reference a deleted function
1>...src\example.cpp(476): message : see declaration of 'StringWrapper::operator ='
1>...src\example.cpp(476,24): message : 'StringWrapper &StringWrapper::operator =(const StringWrapper &)': function was explicitly deleted
具体来说,它建议std::sort
正在尝试引用已删除的复制者,但我不知道为什么会出现这种情况;我希望std::sort
仅使用移动操作。
谁能向我解释一下这是怎么回事?