以下代码无法使用 Visual Studio 2013 编译:
#include
struct X {
X() = default;
X(const X&) = delete;
X& operator=(const X&) = delete;
X(X&&) = delete;
X& operator=(X&&) = delete;
~X() = default;
};
void foo()
{
std::vector v;
std::vector w;
w = std::move(v);
}
错误信息说
error C2280: 'X::X(X &&)' : attempting to reference a deleted function
这对我来说毫无意义。您应该不需要 X
的 move 构造函数为了 move vector
.这是编译器错误,还是我遗漏了什么?
这是完整的错误信息:
C:\Program Files (x86)\Microsoft Visual Studio 12.0\VC\include\xmemory0(600): error C2280: 'X::X(X &&)' : attempting to reference a deleted function
Test.cpp(9) : see declaration of 'X::X'
C:\Program Files (x86)\Microsoft Visual Studio 12.0\VC\include\xmemory0(723) : see reference to function template instantiation 'void std::allocator<_Ty>::construct<_Objty,_Ty>(_Objty *,_Ty &&)' being compiled
với
[
_Ty=X
, _Objty=X
]
C:\Program Files (x86)\Microsoft Visual Studio 12.0\VC\include\xmemory0(723) : see reference to function template instantiation 'void std::allocator<_Ty>::construct<_Objty,_Ty>(_Objty *,_Ty &&)' being compiled
với
[
_Ty=X
, _Objty=X
]
C:\Program Files (x86)\Microsoft Visual Studio 12.0\VC\include\xmemory0(872) : see reference to function template instantiation 'void std::allocator_traits<_Alloc>::construct<_Ty,_Ty>(std::allocator<_Ty> &,_Objty *,_Ty &&)' being compiled
với
[
_Alloc=std::allocator
, _Ty=X
, _Objty=X
]
C:\Program Files (x86)\Microsoft Visual Studio 12.0\VC\include\xmemory0(872) : see reference to function template instantiation 'void std::allocator_traits<_Alloc>::construct<_Ty,_Ty>(std::allocator<_Ty> &,_Objty *,_Ty &&)' being compiled
với
[
_Alloc=std::allocator
, _Ty=X
, _Objty=X
]
C:\Program Files (x86)\Microsoft Visual Studio 12.0\VC\include\xmemory(378) : see reference to function template instantiation 'void std::_Wrap_alloc>::construct(_Ty *,X &&)' being compiled
với
[
_Ty=X
]
C:\Program Files (x86)\Microsoft Visual Studio 12.0\VC\include\xmemory(378) : see reference to function template instantiation 'void std::_Wrap_alloc>::construct(_Ty *,X &&)' being compiled
với
[
_Ty=X
]
C:\Program Files (x86)\Microsoft Visual Studio 12.0\VC\include\xmemory(416) : see reference to function template instantiation '_FwdIt std::_Uninit_copy<_InIt,_FwdIt,std::allocator<_Ty>>(_InIt,_InIt,_FwdIt,std::_Wrap_alloc> &,std::_Nonscalar_ptr_iterator_tag)' being compiled
với
[
_FwdIt=X *
, _InIt=std::move_iterator<><><>>>>
, _Ty=X
]
C:\Program Files (x86)\Microsoft Visual Studio 12.0\VC\include\xmemory(427) : see reference to function template instantiation '_FwdIt std::_Uninit_copy<_Iter,X,_Alloc>(_InIt,_InIt,_FwdIt,_Alloc &)' being compiled
với
[
_FwdIt=X *
, _Iter=std::move_iterator<><><>>>>
, _Alloc=std::_Wrap_alloc<>>
, _InIt=std::move_iterator<><><>>>>
]
C:\Program Files (x86)\Microsoft Visual Studio 12.0\VC\include\vector(1640) : see reference to function template instantiation '_FwdIt std::_Uninitialized_copy<_Iter,X*,std::_Wrap_alloc>>(_InIt,_InIt,_FwdIt,_Alloc &)' being compiled
với
[
_FwdIt=X *
, _Iter=std::move_iterator<><><>>>>
, _Ty=X
, _InIt=std::move_iterator<><><>>>>
, _Alloc=std::_Wrap_alloc<>>
]
C:\Program Files (x86)\Microsoft Visual Studio 12.0\VC\include\vector(789) : see reference to function template instantiation 'X *std::vector>::_Ucopy<_Iter>(_Iter,_Iter,X *)' being compiled
với
[
_Ty=X
, _Iter=std::move_iterator<><><>>>>
]
C:\Program Files (x86)\Microsoft Visual Studio 12.0\VC\include\vector(789) : see reference to function template instantiation 'X *std::vector>::_Ucopy<_Iter>(_Iter,_Iter,X *)' being compiled
với
[
_Ty=X
, _Iter=std::move_iterator<><><>>>>
]
C:\Program Files (x86)\Microsoft Visual Studio 12.0\VC\include\vector(766) : see reference to function template instantiation 'void std::vector>::_Construct<_Iter>(_Iter,_Iter,std::forward_iterator_tag)' being compiled
với
[
_Ty=X
, _Iter=std::move_iterator<><><>>>>
]
C:\Program Files (x86)\Microsoft Visual Studio 12.0\VC\include\vector(766) : see reference to function template instantiation 'void std::vector>::_Construct<_Iter>(_Iter,_Iter,std::forward_iterator_tag)' being compiled
với
[
_Ty=X
, _Iter=std::move_iterator<><><>>>>
]
C:\Program Files (x86)\Microsoft Visual Studio 12.0\VC\include\vector(854) : see reference to function template instantiation 'void std::vector>::_Construct<><><><>>>>>(_Iter,_Iter)' being compiled
với
[
_Ty=X
, _Iter=std::move_iterator<><><>>>>
]
C:\Program Files (x86)\Microsoft Visual Studio 12.0\VC\include\vector(854) : see reference to function template instantiation 'void std::vector>::_Construct<><><><>>>>>(_Iter,_Iter)' being compiled
với
[
_Ty=X
, _Iter=std::move_iterator<><><>>>>
]
C:\Program Files (x86)\Microsoft Visual Studio 12.0\VC\include\vector(849) : while compiling class template member function 'void std::vector>::_Assign_rv(std::vector<_Ty,std::allocator<_Ty>> &&,std::false_type)'
với
[
_Ty=X
]
C:\Program Files (x86)\Microsoft Visual Studio 12.0\VC\include\vector(860) : see reference to function template instantiation 'void std::vector>::_Assign_rv(std::vector<_Ty,std::allocator<_Ty>> &&,std::false_type)' being compiled
với
[
_Ty=X
]
Test.cpp(16) : see reference to class template instantiation 'std::vector>' being compiled
với
[
_Ty=X
]
正如 dyp 在评论中提到的,这是一个 reported bug in C++11 *。表达式
a = rv
(其中 Một
Đúng X
类型的容器,元素类型为 T
,而 rv
Đúng X
类型的非常量右值)
在表 99“可识别分配器的容器要求”中具有以下要求:
Nếu như allocator_traits::propagate_on_container_move_assignment
::value
là SAI
, T
là MoveInsertable vào trong X
Và MoveAssignable. All existing elements of Một
are either move assigned to or destroyed.
allocator_traits
propagate_on_container_move_assignment
的定义如下:
định nghĩa kiểu
see below propagate_on_container_move_assignment;
Type: Alloc::propagate_on_container_move_assignment
if such a type exists,
otherwise false_type
.
问题是忘记将相应的 typedef 放入 std::allocator
, 所以 propagate_on_container_move_assignment
一直是 SAI
.只需添加 typedef 即可在 C++14 中解决此问题。
* 注意 [default.allocator] 和 [allocator.traits.types] 实际上在 N3337 的 §20.6 中,而不是 §20.7。
Tôi là một lập trình viên xuất sắc, rất giỏi!