为什么在对std :: vector :: emplace_back()的调用中调用了复制构造函数? [英] Why is copy constructor called in call to std::vector::emplace_back()?
问题描述
据我了解, std :: vector :: emplace_back()
的目的是专门避免调用复制构造函数,并且
It is my understanding that the purpose of std::vector::emplace_back()
is specifically to avoid calling a copy constructor, and instead to construct the object directly.
请考虑以下代码:
#include <memory>
#include <vector>
#include <boost/filesystem.hpp>
using namespace std;
struct stuff
{
unique_ptr<int> dummy_ptr;
boost::filesystem::path dummy_path;
stuff(unique_ptr<int> && dummy_ptr_,
boost::filesystem::path const & dummy_path_)
: dummy_ptr(std::move(dummy_ptr_))
, dummy_path(dummy_path_)
{}
};
int main(int argc, const char * argv[])
{
vector<stuff> myvec;
// Do not pass an object of type "stuff" to the "emplace_back()" function.
// ... Instead, pass **arguments** that would be passed
// ... to "stuff"'s constructor,
// ... and expect the "stuff" object to be constructed directly in-place,
// ... using the constructor that takes those arguments
myvec.emplace_back(unique_ptr<int>(new int(12)), boost::filesystem::path());
}
由于某些原因,尽管使用了 emplace_back()
函数,此代码无法编译,并显示错误:
For some reason, despite the use of the emplace_back()
function, this code fails to compile, with the error:
错误C2248:' std :: unique_ptr< _Ty> :: unique_ptr':无法访问在类'std :: unique_ptr< _Ty>'中声明的私有成员[...]此诊断发生在编译器生成的函数'stuff :: stuff(const stuff& amp; ;)'
请注意,编译器试图创建(和使用)COPY CONSTRUCTOR 。如上所述,据我了解, emplace_back()
的目的是避免使用复制副本的构造函数。
Notice that the compiler attempted to create (and use) the COPY CONSTRUCTOR. As I've discussed above, it's my understanding that the purpose of emplace_back()
is to avoid the use of the copy constructor.
当然,由于编译器试图创建并调用 copy 构造函数,因此代码无法编译即使我为东西
定义了复制构造函数,因为 std :: unique_ptr
不能为在复制构造函数中使用。因此,我非常想避免使用复制构造函数(实际上,我需要避免使用它)。
Of course, since the compiler is attempting to create and call the copy constructor, there's no way the code would compile even if I defined the copy constructor for stuff
, because the std::unique_ptr
cannot be used in a copy constructor. Hence, I would very much like to avoid the use of a copy constructor (in fact, I need to avoid it).
(这是VS 11.0.60610.01 Update 3在Windows 7 64位系统上)
(This is VS 11.0.60610.01 Update 3 on Windows 7 64-bit)
为什么编译器会生成并尝试使用副本构造函数,即使我正在调用 emplace_back( )
?
Why is the compiler generating, and attempting to use, the copy constructor, even though I am calling emplace_back()
?
注意(响应@Yakk的回答):
Note (in response to @Yakk's answer):
按如下所示显式添加move构造函数可以解决问题:
Explicitly adding the move constructor, as follows, resolves the problem:
stuff(stuff && rhs)
: dummy_ptr(std::move(rhs.dummy_ptr))
, dummy_path(rhs.dummy_path)
{}
推荐答案
Visual Studio 2013及更早版本无法为您编写默认的move构造函数。将简单的显式move构造函数添加到 stuff
。
Visual Studio 2013 and earlier fails to write default move constructors for you. Add a simple explicit move constructor to stuff
.
如果将其推入或放回原位,则可能导致其移动需要重新分配,这在您的情况下是复制的,因为东西
没有任何动作。
A push or emplace back can cause stuff to be moved if it needs to reallocate, which in your case copies, as stuff
has no move.
这是msvc错误。
这篇关于为什么在对std :: vector :: emplace_back()的调用中调用了复制构造函数?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!