18

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.

Daniel A. White
  • 187,200
  • 47
  • 362
  • 445
Teodor Nikolov
  • 783
  • 7
  • 14

1 Answers1

22

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.

François Andrieux
  • 28,148
  • 6
  • 56
  • 87