Const references in std::vector elements

c++, constants, reference, stl, vector

Solution

The `vector` elements must be assignable. From section 23.2.4 Class template vector of the C++ standard:

...the stored object shall meet the requirements of Assignable.

Problem

Is it only my compiler or is it forbidden to use cons references in std::vector elements. Consider following structure: ``` struct Y { const int & x; Y(const int & p_x): x(p_x) { } }; ``` Now, when I try to push such object onto vector: ``` std::vector<Y> yv; int x = 5; Y y(x); yv.push_back(y); ``` I get compiler error: "error: non-static reference member `const int&Y::x', can't use default assignment operator". Shouldn't copy ctor be enough?

Original source