C++ Logo

std-proposals

Advanced search

Re: [std-proposals] std::elide

From: Frederick Virchanza Gotham <cauldwell.thomas_at_[hidden]>
Date: Sat, 25 May 2024 17:16:11 +0100
On Sat, May 25, 2024 at 3:24 PM Thiago Macieira wrote:
>
> This is what I am asking of Frederick: give us three distinct examples of
> situations we'd want to return modified immovable types. Maybe expand to more
> use-cases where access to the return value slot would be helpful.


I admit defeat on this one.

I was writing DOS batch files up until 1998, and then I took up Visual
Basic, and then four years later I moved on to C++. Since then, I've
programmed desktop PC's (ARM, Microsoft, Apple, Linux),
microcontrollers (PIC, Arduino, Atmel, Texas Instruments), embedded
Linux (ARM, x86), a device driver for BareBox, and written assembler
for about 6 or 7 instruction sets. In all that time, so we're talking
around about 2.5 decades, I've never needed RVO nor NRVO with an
unmovable-and-uncopyable type. If ever there was a situation when I
nearly needed it, I probably just used a char buffer with
placement-new, or maybe I used "std::aligned_union" back before it was
deprecated. Nowadays I'd probably use std::optional.

If my hypothetical boss were to call me today having a nervous
breakdown and say, "Look I know I shouldn't call you on a Saturday
about work, but my hair is falling out here and we're going to lose
the contract and everyone's going to get laid off ... so is there some
way you can provide a C++ source file containing an implementation of
a function that returns a locked mutex by value?". I'd ask her what it
needs to compile on, and if she came back with "It's going in the main
firmware so it has to work on the two microcontrollers and also on the
desktop PC simulators too". Well I'd give her this:

    mutex Func(void)
    {
        struct S {
            mutex m;
            S() { m.lock(); }
        };
        static_assert( sizeof (S) == sizeof (mutex) );
        static_assert( alignof(S) == alignof(mutex) );
        constexpr auto pf1 = +[](){ return S(); };
        auto const pf2 = *static_cast<mutex(*const
*)()>(static_cast<void const*>(&pf1));
        return pf2();
    }

Then she'd say to me, "Isn't it undefined behaviour to invoke that
lambda after you've casted it?", and I'd respond "I won't tell anyone
if you don't". The above function will work as intended on every C++
compiler, irrespective of whether it's a 16-bit-byte microcontroller
or a 40-core desktop PC running Windows 11.

I don't need NRVO but I'm certainly enjoying the technical discussion
as to how we would implement it. This mailing list is a hobby for me.
But maybe other guys here have a genuine need for NRVO with
unmovable-and-uncopyable types (it sounds like a few people do).

Received on 2024-05-25 16:16:23