Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

Trying to get std::launder to actually do something

16 views
Skip to first unread message

Frederick Virchanza Gotham

unread,
Aug 18, 2023, 12:42:58 PM8/18/23
to

C++17 introduced std::launder, and C++23 introduced std::start_lifetime_as, so it seems the Standards guys are really taking the 'object model' seriously.

Amidst all this seriousness though, those two functions are no-op's for most compilers.

Enthusiasts tried to find a situation in which std::launder actually does something, and they only found one example of C++ code that behaves
differently when you use 'launder', and it involved a constructor using
'placement new' to invoke a constructor belonging to another class on the
current object, something like:

MyClass::MyClass(void)
{
::new(this) SomeOtherClass();
}

The use of 'launder' on the pointer to this object prevented caching of the vtable, you can see what they did here:

https://miyuki.github.io/2016/10/21/std-launder.html

I achieved something similar by marking the object as 'volatile' in a puzzle I composed on Codewars:

https://www.codewars.com/kata/6359c81e00fba2852618a1cb/

I've been mulling over the idea though of using 'placement new' inside a class's constructor in order to change its vtable. Can anyone think of any weird, wild and wacky uses of this?

Alf P. Steinbach

unread,
Aug 18, 2023, 2:04:02 PM8/18/23
to
On 2023-08-18 6:42 PM, Frederick Virchanza Gotham wrote:
> Enthusiasts tried to find a situation in which std::launder actually does something

Would be nice with a link to that.

- Alf

Pavel

unread,
Aug 18, 2023, 9:04:39 PM8/18/23
to
I can only think of a reason why finding a good use for this is
difficult -- because changing "a vtable" or, in impl-independent
paralnce, "a dynamic type" is exactly what creating an object of a
derived class does, likely better.

-Pavel



0 new messages