C++ Logo

std-proposals

Advanced search

Re: [std-proposals] Replace an object -- but retain old object if new object fails to construct

From: Giuseppe D'Angelo <giuseppe.dangelo_at_[hidden]>
Date: Thu, 30 Oct 2025 16:15:33 +0100
Il 30/10/25 15:27, Thiago Macieira via Std-Proposals ha scritto:
> I'll accept a replacement function if realloc() can't be fixed. But there needs
> to be something that resizes an allocated memory block for trivially-
> relocatable objects and has a good chance of not moving the memory block
> (resize-in-place). Was that added by P2786?

No. P2786 / trivial relocation as per the current working draft doesn't
grant any special features to realloc(). std::trivially_relocate is the
only entry point for trivial relocation.

 From my understanding, P3858 (likely C++29 material at this point)
wants to correct this by introducing a lower-level primitive than
std::trivially_relocate.

The idea is that you will be able to move object representations in
memory using *any* facility you like (including realloc()), but then you
need to call std::restart_lifetime<T>() to tell the abstract machine¹
that there are objects of type T at the destination whose lifetime needs
to be started.

I'm also 99% sure that P3858 does not solve the problem of detecting
bitwise trivial relocability (detection that we need for things like
QVariant), but that's a separate can of worms...

My 2 c,


¹ But also the physical machine: if T is polymorphic / contains
polymorphic subobjects, it will deal with the vtable re-signing on arm64e.

--
Giuseppe D'Angelo | giuseppe.dangelo_at_kdab.com | Senior Software Engineer
KDAB (France) S.A.S., a KDAB Group company
Tel. France +33 (0)4 90 84 08 53, http://www.kdab.com
KDAB - Trusted Software Excellence

Received on 2025-10-30 15:15:36