“Appends a new element to the end of the container. The element is
constructed in-place,i.e. no copy or move operations are performed.
The constructor of the element is called with exactly the same
arguments that are supplied to the function.”
以下示例:
#include <vector> struct A { A(int){} A(A const&) = delete; }; int main() { std::vector<A> vec; vec.emplace_back(1); return 0; }
在线vec.emplace_back(1); Visual Studio 2013 / GCC报告:
error C2280: ‘A::A(const A &)’ : attempting to reference a deleted function
error: use of deleted function ‘A::A(const A&)’
错误是否正确?你能解释一下为什么吗
解决方法
Expression:
a.emplace_back(args)
Return type:
void
Operation semantics: Appends an object of type
T
constructed withstd::forward<Args>(args)...
. Requires:T
shall beEmplaceConstructible
intoX
fromargs
. Forvector
,T
shall also beMoveInsertable
intoX
.
您的A不符合MoveInsertable要求,因为您没有移动构造函数,只有一个已删除的副本构造函数.与std :: vector,it works之外的容器.