lundi 26 décembre 2016

Why map's elements don't need copy or move constructors but vector's do?

I have this class:

class Object {
public:
    Object();
    Object(const Object&) = delete;
    Object& operator=(const Object&) = delete;
    ~Object();
};

  • The default copy constructor & assign operator is deleted,

  • no move constr./assign op. declared,

  • and there is also no implicit move constr./assign op. because there is a destructor declared.

When I try to store it in a std::vector:

std::vector<Object> objects;
objects.emplace_back();

It gives a compile error because Object doesn't have a copy constructor/move c./etc.

  • (it is needed because on resizing the objects have to be copied/moved to the new container)

But when I try to store it in an std::map:

std::map<int, Object> objects;
objects.emplace(std::piecewise_construct,
    std::make_tuple(1),
    std::make_tuple()
);

It works perfectly. Why?

Aucun commentaire:

Enregistrer un commentaire