stdlaunder

Why introduce `std::launder` rather than have the compiler take care of it?

孤人 提交于 2021-02-17 22:01:00
问题 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<sizeof(int), alignof(int)>::type data; new(&data) int; int *p = std::launder(reinterpret_cast<int*>(&data)); [basic.life]/8 tells us that, if you allocate a new object in the storage of the old one, you cannot access the new object through pointers to the old. std::launder allows us to side-step that. So, why not

placement new on a class with reference field

本小妞迷上赌 提交于 2020-06-25 03:15:33
问题 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 ends new (this) C(other); // new object of type C created f(); // well-defined } return *this; } int main() { C c1; C c2; c1 = c2; // well-defined c1.f(); // well-defined; c1 refers to a new object of type C } Would the following be legal or undefined behavior : struct C {

What is the purpose of std::launder?

巧了我就是萌 提交于 2019-11-26 21:17:55
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 to be aware of? And what are we launder ing? Nicol Bolas std::launder is aptly named, though only if you know what it's for. It performs memory laundering . Consider the example in the paper: struct X { const int n; }; union U { X x; float f; }; ... U u = {{ 1 }}; That statement performs aggregate initialization, initializing the first member of U

What is the purpose of std::launder?

≯℡__Kan透↙ 提交于 2019-11-26 06:54:25
问题 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 to be aware of? And what are we launder ing? 回答1: std::launder is aptly named, though only if you know what it's for. It performs memory laundering . Consider the example in the paper: struct X { const int n; }; union U { X x; float f; }; ... U u =