I cannot compile the following dont_compile function. I don't understand why it doesn't work. But, it does work with list.
class Thing {
public:
Thing() {}
Thing(const Thing &) = delete;
};
int dont_compile(int argc, char ** argv)
{
std::vector<Thing> v;
v.emplace_back();
return 0;
}
int compiles(int argc, char ** argv)
{
std::list<Thing> v;
v.emplace_back();
return 0;
}
Here is the error from the compiler. Is it a bug?
/Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/bin/../include/c++/v1/memory:1752:31: error: call to deleted constructor of 'Thing'
::new((void*)__p) _Up(_VSTD::forward<_Args>(__args)...);
^ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
... snip ...
note: 'Thing' has been explicitly marked deleted here
Thing(const Thing &) = delete;
I don't really understand how _Up(...) is causing the copy constructor to be invoked.
std::vector::emplace_back requires the type of the vector to be EmplaceConstructible as well as MoveInsertable. Since you delete the copy constructor and do not define a move constructor yourself, Thing does not satisfy the second requirement. In contrast, std::list::emplace_back only requires the list type to be EmplaceConstructible.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With