4

In the code below, 2 pointers are set to point at members of a temporary std::vector. The contents of that vector are then std::move()'d into another container... After the move the pointers appear to still be valid (they print the right values and valgrind shows no problem) but can I rely on this behavior? Does the spec say what happens in cases like these?

#include <vector>
#include <iostream>

int main( int argc, char* argv[] )
{
    int *p5, *p10;
    std::vector<int> bufB;

    {
        std::vector<int> bufA = { 1, 2, 3, 4, 5, 6, 7, 8, 9, 10 };
        p5 = &bufA[4];
        p10 = &bufA[9];
        bufB = std::move( bufA );
    }

    // Does p5 point at the guts of bufB now?

    std::cout << "*p5 = " << *p5 << std::endl;
    std::cout << "*p10 = " << *p10 << std::endl;

    return 0;
}
dicroce
  • 45,396
  • 28
  • 101
  • 140

0 Answers0