C++ Logo

std-discussion

Advanced search

Re: std::launder question

From: language.lawyer_at <language.lawyer_at_[hidden]>
Date: Tue, 22 Feb 2022 00:56:37 +0300
On 22/02/2022 00:35, Ryan Nicholl via Std-Discussion wrote:
> Assuming that we allocate some storage such that there is space for multiple T e.g.
>
> T* foo_array = (T*) malloc(sizeof(foo)*20);
>
> Then if we construct
>
> new (foo_array+5) T(...);
> then
> *std::launder(foo_array+5)
> is valid
>
> But is
>
> std::launder(foo_array)[5]
>
> also valid?

Depends on T. If it is not an implicit-lifetime type, then you would violate https://timsong-cpp.github.io/cppwp/n4868/ptr.launder#2.sentence-2
Also, you don't need std::launder in the first case.

Received on 2022-02-21 21:56:40