C++ Logo

sg12

Advanced search

Re: [ub] new revision of p0593

From: Richard Smith <richardsmith_at_[hidden]>
Date: Fri, 09 Feb 2018 22:54:10 +0000
On Fri, 9 Feb 2018 at 14:15, Jens Maurer <Jens.Maurer_at_[hidden]> wrote:

> On 02/09/2018 08:52 PM, Richard Smith wrote:
> > Please find attached a revised version of P0593
>
> Regarding std::bless in particular: While that's probably
> a useful facility for stuff such as mmap, I'd actually
> like to see a
>
> T * result = std::bless<T>(p)
>
> which works as-if an object of type T were created at p,
> reusing the existing values of the storage.
>

I think that's:

std::bless(p, sizeof(T));
T *result = std::launder(reinterpret_cast<T*>(p));

... assuming T is an implicit lifetime type. Packaging that up as a
separate library function might be nice, though (your form is probably
easier for humans and tools to reason about). Especially since it means we
can enforce the implicit lifetime type requirement (otherwise I think
people would be tempted to use this to implement things like node_handle,
and it's not quite strong enough for that in general).

Received on 2018-02-09 23:54:24