C++ Logo

std-proposals

Advanced search

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

From: Frederick Virchanza Gotham <cauldwell.thomas_at_[hidden]>
Date: Thu, 18 Jul 2024 10:13:57 +0100
On Thu, Jul 18, 2024 at 7:24 AM Lénárd Szolnoki wrote:
>
> This is not ideal, as this pessimizes the codegen compared to what is
> possible with P2025. In the P2025 version when `w.some_observer()` is
> false, but otherwise no exception is thrown then there is no exception
> being thrown and caught, other than potential exceptions thrown by
> Widget member functions.
>
> I would like to match this clang codegen here:
>
> https://godbolt.org/z/nYsn1qG98
>
> If refactoring this NRVO into guaranteed copy elision through
> std::factory (or std::construct_modify) pessimizes this codegen then
> that's not very compelling for the proposal.


The current GodBolt we have for the the next revision of P3357 is:

    https://msvc.godbolt.org/z/n87M3WYzb

Look at the class called 'Clone', specifically the body of the
constructor at Line #70. (Previous this class was named 'Derived' but
I renamed it because it didn't make sense as we don't use
inheritance).

I have further tweaked the above GodBolt this morning. All of the line
numbers are incremented by 1 because I add an inclusion directive for
<memory> in order to use "construct_at":
    Line #65 ---- "T var" is now "alignas(T) std::byte[sizeof(T)]"
    Line #66 ---- I've added a destructor that explicitly
destroys the object
    Lines #75 - 95 ---- If the 'setup' lambda returns false,
'var' is explicitly destroyed, and there's space there to do something
else such as call another lambda.

Here is the tweaked GodBolt this morning:

    https://msvc.godbolt.org/z/6djhn6PYs

It can be adapted a little to achieve something along the lines of:

  Widget foo(int const i)
  {
    return construct_modify<Widget>(
        i,
        [](auto &a) -> bool { return a.some_observer; },
        [](auto &a) -> Widget { return Widget(0); }
    );
  }

What do you reckon?

Received on 2024-07-18 09:14:09