I can't figure out how push_back(const value_type& val)
exactly works, in docs it says about val
that
val is Value to be copied (or moved) to the new element ...
How it can be copied when it takes
val
by reference ?Will that copying ever call the copy constructor of
val
?
and what's exactly happening here ?
#include <iostream>
#include <vector>
using namespace std;
struct x
{
x(int v = 0) : v(v) {}
int v;
};
vector<vector<x>> parts;
void fillParts()
{
vector<x> values = { x(1), x(2), x(3) };
parts.push_back(values);
}
int main()
{
fillParts();
parts[0][0].v = -123;
cout << parts[0][0].v; // -123
return 0;
}
this runs with no erros,
is parts[0]
is a reference to local vector values
or a copy ?
if it is a reference shouldn't it at least give some warnings saying that your accessing and modifying local objects of freed stack ?