Search code examples
c++c++11vectorconstantsemplace

Emplacing an instance with constant fields fails to compile (C++)


I have an example_class with two constant fields. When I try to emplace an object of that class into a std::vector, I get various errors such as "error: object of type 'example_class' cannot be assigned because its copy assignment operator is implicitly deleted". Does anyone know what is going on here? If I remove the const in example class, the code compiles without problems.

#include <vector>
#include <iostream>

using namespace std;

class example_class {
    public:
    example_class(int v1, int v2) : val1(v1), val2(v2) {}

    private:
    const int val1, val2;
};

int main() {

    vector<example_class> *vec = new vector<example_class>();
    vec->emplace(vec->begin() + 1, 13131, 12313);

    return 0;
}

Solution

  • By trying to use the std::vector<T>::emplace in this case we get something like: error: use of deleted function ‘Foo& Foo::operator=(Foo&&)’ which follows from the fact that...

    ...if the required location (e.g. vec.begin()) has been occupied by an existing element, the inserted element is constructed at another location at first, and then move assigned into the required location - More here

    The example_class cannot be move assigned due to presence of const data members.

    You still can construct elements directly in the vec by making sure that you not trying to construct them in place of any already existing element - by using `emplace_back().

    More importantly, note that in vec.begin()+1 you try to access beyond the currently allocated memory for the vec (which is empty, meaning vec.begin() == vec.end()). It's the same as assuming that subscripting an element after the end of a vector will add an element to it - which is wrong.