C++ Logo

std-proposals

Advanced search

Re: [std-proposals] Stop gap required for NRVO until Anton's paper is assimilated

From: Lénárd Szolnoki <cpp_at_[hidden]>
Date: Thu, 18 Jul 2024 10:55:18 +0100
On 18/07/2024 10:16, Frederick Virchanza Gotham via Std-Proposals wrote:
> On Thu, Jul 18, 2024 at 10:13 AM Frederick Virchanza Gotham wrote:
>>
>> Widget foo(int const i)
>> {
>> return construct_modify<Widget>(
>> i,
>> [](auto &a) -> bool { return a.some_observer; },
>> [](auto &a) -> Widget { return Widget(0); }
>> );
>> }
>
>
> Correction, that should be:
>
> Widget foo(int const i)
> {
> return construct_modify<Widget>(
> i,
> [](auto &a) -> bool { return a.some_observer(); },
> [](void ) -> Widget { return Widget(0); }
> );
> }

`if` is only one possible control flow that is affected. The same reuse
of the return slop can happen in a loop too.

https://godbolt.org/z/xebafoG5P

Widget foo(int x) {
   for (int i = 0; i < x; ++i) {
     auto w = Widget(i);
     if (w.some_observer()) {
         return w;
     }
   }
   return Widget(-1);
}

So I don't like your proposed direction here, which is essentially
reinventing control flow in the library, starting with `if`.

Cheers,
Lénárd

Received on 2024-07-18 09:55:21