In the code below, whenever a new member is inserted to the std::vector<int>A
, due to memory re-allocations, std::vector<reference_wrapper<int>>B
points to a wrong address. Is it possible to make the referencing vector to track the re-allocations and store the correct address always?
#include <iostream>
#include <functional>
#include <vector>
using namespace std;
int main()
{
vector<int> A (0,3);
vector<reference_wrapper<int>> B;
B.push_back ( ref(A[0]) );
B.push_back ( ref(A[1]) );
B.push_back ( ref(A[2]) );
A.push_back (0);
cout << &A[0] << endl;
cout << &B[0].get() << endl;
return 0;
}