假设我们有一个简单的类,其中包含一个
具体而言,它表明
有人能否解释一下这里发生了什么?
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
只使用移动操作。有人能否解释一下这里发生了什么?
operator=
。当一个类有const
成员时,它不会生成赋值运算符。 - Pete Becker= default
:在这种情况下,默认的移动运算符也被删除。 - Mooing DuckStringWrapper
对象不可排序,因为它们不可分配和移动分配。您需要提供一个可移动分配的实现。 - Eljayoperator<
还是另一个函数。 - Justin Time - Reinstate Monicaconst
成员默认被删除,这是有道理的。 - Dan