Is the front address of std::vector move invariant?
Asked Answered
M

1

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.

Meathead answered 25/7, 2018 at 19:34 Comment(0)
A
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.

Abebi answered 25/7, 2018 at 19:37 Comment(2)
Hm, and is there no additional guarantee for end Iterators of a std::vector?Wareroom
cppreference I think, not cccpreference. The latter should be for fans of the former USSR ;)Overwhelm

© 2022 - 2025 — McMap. All rights reserved.