C++ Logo

std-proposals

Advanced search

Re: [std-proposals] Relocation in C++

From: Sébastien Bini <sebastien.bini_at_[hidden]>
Date: Wed, 8 Jun 2022 10:34:18 +0200
Hi all,

Well, yes; certainly arguing about the exact spelling is bikeshedding. One
> aspect I think needs further exploration is a similar facility to allow
> running code before the relocation of subobjects begins, and I think
> constructor delegation could be the facility to solve that; the other
> aspect is symmetry with operator=(T).
>

The delegating constructor will not have the ability to do memberwise
relocation and to avoid the destructor call. As such using the delegating
constructor to inject code right before relocation will lose the benefit of
having a relocation ctor in the first place. Why would you need to have
that option? We don't support that for any other ctor (custom code before
member initialisation).

As for operator=(T), what do you have in mind? I am satisfied with leaving
it implementation defined, users can freely use destruct + relocate or use
swap at their own convenience.

void sink(T);
>> void sink(T&&);
>>
>> void foo(T val)
>> {
>> sink(reloc val);
>> }
>>
>> What happens now if T has opted-out of the ABI break with the
>> non-standardized attribute? Before the reloc operator was in charge of
>> building the new instance and could fallback to the move constructor. Now
>> this is no longer the case as it depends on how the prvalue is consumed.
>> Isn't it problematic as now the language needs to distinguish between
>> prvalues whose destructor calls can be alleviated and the others? Which
>> overload of sink is picked in that case?
>>
>
> The ABI break opt-out doesn't affect overloading, so sink(T) will be
> called in either case. Supposing T has the attribute applied, the move or
> copy constructor of T will be called to construct a temporary from val and
> this temporary is the argument to sink(T); this is much as would happen
> today, and is exactly the same if T does not have a candidate relocation
> operator/relocating constructor.
>

Another way to solve this is to state that `reloc obj` may turn the lvalue
into a prvalue *or an xvalue* *if obj is a function parameter*. That choice
would be implementation defined. As such, if obj had opted out of the ABI
break then `reloc obj` would simply turn obj to an xvalue, no temporary
would be created and sink(T&&) would be called. Otherwise, in the nominal
case (obj is okay with the ABI break), then reloc turns obj into prvalue
and sink(T) is called.

Best regards,
Sébastien

Received on 2022-06-08 08:34:30