Search code examples
c++vectorstdmove

Is the front address of std::vector move invariant?


In the following snippet:

std::vector<double> a(100, 4.2);
auto* a_ptr = a.data();

auto b = std::move(a);
auto* b_ptr = b.data();

std::cout << ((b_ptr == a_ptr) ? "TRUE" : "FALSE") << '\n';

does the C++ standard guarantee that b_ptr is always equal to a_ptr after std::move? Running the code on wandbox prints TRUE.


Solution

  • From cppreference.com :

    After container move construction (overload (6)), references, pointers, and iterators (other than the end iterator) to other remain valid, but refer to elements that are now in *this. The current standard makes this guarantee via the blanket statement in §23.2.1[container.requirements.general]/12, and a more direct guarantee is under consideration via LWG 2321.

    Pointers to elements are not invalidated, including pointers to the first element.