Questions tagged [stdlaunder]
25 questions
360
votes
3 answers
What is the purpose of std::launder?
P0137 introduces the function template std::launder and makes many, many changes to the standard in the sections concerning unions, lifetime, and pointers.
What is the problem this paper is solving? What are the changes to the language that I have…

Barry
- 286,269
- 29
- 621
- 977
34
votes
1 answer
Why introduce `std::launder` rather than have the compiler take care of it?
I've just read
What is the purpose of std::launder?
and frankly, I am left scratching my head.
Let's start with the second example in @NicolBolas' accepted answer:
aligned_storage::type data;
new(&data) int;
int *p =…

einpoklum
- 118,144
- 57
- 340
- 684
17
votes
1 answer
How to interpret the reachability requirement of std::launder?
The std::launder function requires that every byte that would be reachable through the result is reachable through the argument. "Reachable" is defined as follows:
A byte of storage is reachable through a pointer value that points to an object Y if…

Brian Bi
- 111,498
- 10
- 176
- 312
14
votes
1 answer
std::launder alternative pre c++17
It is like std::optional, but doesn't store an extra bool. User has to make sure to access only after initializing.
template
union FakeOptional { //Could be a normal struct in which case will need std::aligned storage object.
…

balki
- 26,394
- 30
- 105
- 151
12
votes
2 answers
placement new on a class with reference field
This is a code example from the C++20 spec ([basic.life]/8):
struct C {
int i;
void f();
const C& operator=( const C& );
};
const C& C::operator=( const C& other) {
if ( this != &other ) {
this->~C(); // lifetime of *this…

Amir Kirsh
- 12,564
- 41
- 74
9
votes
1 answer
Does placement new start object's lifetime?
The following code example is from cppreference on std::launder:
alignas(Y) std::byte s[sizeof(Y)];
Y* q = new(&s) Y{2};
const int f = reinterpret_cast(&s)->z; // Class member access is undefined behavior
It seems to me that third line will…

DoctorMoisha
- 1,613
- 14
- 25
7
votes
0 answers
Is std::launder required when performing arithmetic as well?
One of the preconditions on std::launder requires that object is within its lifetime. I assume that it is a necessary condition for being able to dereference the element. Does it mean, that if I obtain a pointer to to an array element, then no…

Myrddin Krustowski
- 701
- 3
- 13
7
votes
1 answer
std::launder use cases in C++20
[1]
Are there any cases in which the addition of p0593r6 into C++20 (§ 6.7.2.11 Object model [intro.object]) made std::launder not necessary, where the same use case in C++17 required std::launder, or are they completely orthogonal?
[2]
The example…

Amir Kirsh
- 12,564
- 41
- 74
6
votes
2 answers
Can I overwrite a const object via placement-new?
Basic.life/8 tells us that we can use the storage occupied by an object to create a new one after its lifetime has ended and use its original name to refer to it unless:
the type of the original object is not const-qualified, and, if a class type,…

Fureeish
- 12,533
- 4
- 32
- 62
6
votes
1 answer
Safe (and costless) reinterpretation of sized data
I wanted to write my own "small vector" type, and the first hurdle has been figuring out how to implement the on-stack storage.
I stumbled upon std::aligned_storage, which seems purpose-designed for implementing arbitrary on-stack storage, but I'm…

lcmylin
- 2,552
- 2
- 19
- 31
5
votes
0 answers
std::launder vs placement-new reachability condition
std::launder has a precondition that all bytes reachable from the would-be-returned pointer are reachable through the passed pointer.
My understanding is that this is meant to allow compiler optimizations, so that e.g.
struct A {
int a[2];
…

user17732522
- 53,019
- 2
- 56
- 105
5
votes
1 answer
How to interpret the precondition of std::launder?
struct X { int n; };
const X *p = new const X{3}; // #1
new (const_cast(p)) const X{5}; // #2
const int c = std::launder(p)->n;
Assume that the object created at #1 is named obj1 while the object created at #2 is named obj2. The precondition…

xmh0511
- 7,010
- 1
- 9
- 36
5
votes
1 answer
Does the effect of std::launder last after the expression in which it is called?
Consider the following sample code:
struct X { const int n; };
union U { X x; float f; };
void fun() {
U u = {{ 1 }};
u.f = 5.f; // OK, creates new subobject of 'u'
X *p = new (&u.x) X {2}; // OK, creates new subobject of 'u'
…

John Z. Li
- 1,893
- 2
- 12
- 19
5
votes
1 answer
Why is std::launder required here?
I was reading cppreference and in the example for std::aligned_storage is had this example of a vector/array class:
template
class static_vector
{
// properly aligned uninitialized storage for N T's
typename…

Zebrafish
- 11,682
- 3
- 43
- 119
3
votes
0 answers
Constructing a const object over itself
In the following example I ended the life of s (by calling its dtor.), and after that at the same location I created a new object of the same type S. Since I violated [basic.life]/8.3 (s is const), in order to access the newly created S I have to…

Dr. Gut
- 2,053
- 7
- 26