Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why does vector<T>::emplace_back, such that T has a deleted copy constructor, fail to compile?

Tags:

c++

back

emplace

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.

like image 478
ioquatix Avatar asked Dec 31 '25 10:12

ioquatix


1 Answers

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.

like image 53
Ben Steffan Avatar answered Jan 04 '26 00:01

Ben Steffan



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!