Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

`std::launder` not returning correct data for Clang and GCC but is for msvc

Why doesn’t std::launder return the correct value (2) in CLANG and GCC when the object is in the stack v in the heap? Even using std::launder.
std::launder is required. See this which says launder
is needed when replacing an object const qualified at the top level. This is because
basic.life disallows replacing complete const objects without std::launder, only
sub-objects.

#include <memory>
#include <iostream>

int main()
{
struct X { int n; };
const X *p = new const X{1};
const X x{1};

std::construct_at(&x, X{2});        // on stack
const int c = std::launder(&x)->n;

std::construct_at(p, X{2});         // allocated with new
const int bc = std::launder(p)->n;

std::cout << c << " " << '\n';
std::cout << bc << " " << '\n';
}

See Compiler Explorer.

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

>Solution :

std::construct_at(&x, X{2}); has undefined behavior.

It is not allowed to create a new object in storage that was previously occupied by a const complete object with automatic, static or thread storage duration. (see [basic.life]/10)

Other than that you are correct that std::launder is required in the second case for the reasons you explained: Because the object you created with new is const-qualified. If it weren’t for that the old object would be transparently replaceable with the new one.

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading