2

It seems I don't fully understand how exactly C++ references work. Trying to run following snippet:

#include <vector>
#include <iostream>

int main() {
    std::vector<int> test{1,2,3};
    int& ref = test.back();
    auto lambda = [&ref, &test](){
        std::cout << "inside lambda " << ref << std::endl;
        ref += 1;
        for (auto&v : test) { std::cout << v << " "; }
    };

    lambda(); lambda(); lambda();
    test.push_back(5);
    lambda(); lambda(); lambda();
}

And got this result:

inside lambda 3                                                                                                                                                                       
1 2 4                                                                                                                                                                                 
inside lambda 4                                                                                                                                                                       
1 2 5                                                                                                                                                                                 
inside lambda 5                                                                                                                                                                       
1 2 6                                                                                                                                                                                 
inside lambda 6                                                                                                                                                                       
1 2 6 5                                                                                                                                                                               
inside lambda 7                                                                                                                                                                       
1 2 6 5                                                                                                                                                                               
inside lambda 8                                                                                                                                                                       
1 2 6 5  

Why after push_back is done, none of vector elements is incremented? And where ref points to from this moment?

einpoklum
  • 118,144
  • 57
  • 340
  • 684
Uroboros
  • 189
  • 2
  • 12

1 Answers1

3

At start, ref references the third element of the storage of test. After pushing back a new element it happens that the vector reallocated its storage, so that the old ref element is no more valid. Your code leads to undefined behaviour.

Jean-Baptiste Yunès
  • 34,548
  • 4
  • 48
  • 69