[0] https://stackoverflow.com/a/70419156 (first part)
[1] http://www.open-std.org/jtc1/sc22/wg21/docs/papers/2019/p197...
So is launder needed for anything now?
The problematic code was this:
struct X { const int n; };
union U { X x; float f; };
...
U u = {{ 1 }};
X *p = new (&u.x) X {2};
cout << u.x.n; //is this UB?
After the placement new, we are in the case from this piece of the standard ("the lifetime of an object has ended and before the storage which the object occupied is reused or released, a new object is created at the storage location which the original object occupied"). u.x is "the name of the original object" - can we use it to manipulate the new object that placement new put in there?In the old standard, we can if "the type of the original object [...] does not contain any non-static data member whose type is const-qualified". Since X::n is const qualified, u.x can't be used to refer to the new object - enter std::launder.
However, the new standard modifies this exception: we can still use u.x if "the original object is neither a complete object that is const-qualified nor a subobject of such an object". Neither u nor u.x are const-qualified objects, nor are they a sub-object of a const-qualified object. So, u.x now refers to the new object created by placement new.
Still, std::launder seems to be needed for other cases, mostly related to reinterpret_cast. The C++ Reference site [0] shows these examples.
[0] https://en.cppreference.com/w/cpp/utility/launder#Example
Because the member is const.
It's a bit strange example because there's no reason to have mutable pointer to a struct with only const members. A better example would maybe be:
struct foo{ int const x; int y; };
void some_func(foo *f) {
foo.y = 0;
}
int bar() {
foo f { 123, 456 };
some_func(&f);
return f.x; // must always return 123
}
EDIT: On second thought I'm also a bit confused. Wouldn't this be a "safe" way of modifying `f.x`? void some_func(foo *f) {
*foo = foo { 5, 5 }; // probably wrong syntax? I don't write so much C/C++
} void some_func(foo *f) {
new(f) foo { 5, 5 };
}
because no assignment operator is generated for a struct with a const member.I wanted to use const fields to enforce the logical invariant that a field never changes for the lifetime of an object, until the entire object is overwritten by a newly constructed object. Unfortunately C++ deleting the move-assignment operator means it's no longer practical to do so (though you can use placement-new for POD data where it's safe to not run the destructor, and even then it's obscure and will leak memory and possibly is UB as soon as you add an owning type). Instead, I keep a mutable field and have to remember to never mutate it when writing hundreds of lines of method implementations, and when I come back to the code months later and edit it, and someday I may slip up.
Rust has no const fields at all.
Also, since you have a local pointer f within some_func, so re-assigning a new object of type foo to it will not alter the original foo. This is not implied by your original comment but it's something one could wonder.
I sat next to a guy on the C++ Standards Committee and he was the first to admit that he didn't even fully understand C++. The abstract machine is monstrously complex.
void some_func(foo *p) {
new(p) foo { 42 };
}
That’s not UB.> if you allocate a new object in the storage of the old one, you cannot access the new object through pointers to the old
Which implies the UB happens in the caller.
The access in the caller -- `return f.x` -- is not through a pointer.
As for why the spec prohibits the modification of const values: one of the reasons for compilers being allowed to treat const values as being truly immutable is for performance. If the compiler can see the actual value then it can avoid the memory read entirely, and if it doesn't know the actual value then it only needs to do one memory read.
If those const values could change without the compiler being aware then it now needs to do an awful lot more fetching from memory as it cannot guarantee that the value did not change. Some of it could be optimized away, but it would still make code a lot slower if - for example: mixing reads from const ints with pointer/reference manipulation of ints - as due to potential aliasing the compiler would likely have to read those const ints from memory each time.
Launder let you discard any provenance info for the old pointer and treat it as a new one.
Tricky and very ill-defined, and according to the another comment elsethread this now doesn't require launder anymore (it probably was proven unworkable in practice).
I like to think I know quite a bit about C++, but I don’t get this at all. Why is f.x a const object even when accessed through a non-const variable? f isn’t const; there’s no const_cast here. At first glance, this looks like a bug in the spec, roughly like confusing
(pointer to const) with (const pointer to const). Only the latter will always point to the same thing.What am I missing?
(Edit: Since it’s a bit hidden in the SO question, the idea is that some_func does something like this:
Note that there’s no const_cast here, and f’s storage isn’t const, so this isn’t UB to the best of my knowledge.BTW, in case it’s not obvious, it’s not all that helpful to reply to the question why the spec is a certain way by tautologically repeating the spec.)
(Edit 2: Unfortunately also very typical for C++ discussions, I just love how people think that C++ confusing me must mean that I’m dumb, and then condescendingly post a wrong answer. Rule of thumb: If you’re confident in your ability to understand C++, you don’t understand it very well.)