C++ Logo

std-proposals

Advanced search

Re: [std-proposals] std::elide

From: Frederick Virchanza Gotham <cauldwell.thomas_at_[hidden]>
Date: Mon, 27 May 2024 10:43:33 +0100
On Mon, May 27, 2024 at 9:56 AM Lorand Szollosi wrote:
>
> The proposed std::factory<T>(params…, setup) can be
> written as T{args…} @ setup() as per the ideas I’ve shown
> above.


If we were designing a new programming language from scratch, that
might be a nice idea. But today in 2024 making changes to C++, I think
it's an extreme change to the core language.

It's always going to be easier to convince the committee to add
something to the standard library, than to change the core language.
Plus, core language changes are a lot of work for compiler vendors.

Also just now I had another idea with regard to the "Return Slot"
idea. Previously I proposed a change to the core language by adding a
new keyword '_ReturnSlot", however I was thinking that it would be
possible to pull this off without a core language change. So we start
off with the function we want:

    mutex Func(void)
    {
        mutex m;
        m.lock();
        return m;
    }

Then we change it so that it returns void:

    void Func(void)
    {
        mutex m;
        m.lock;
        return m;
    }

Then we change it so that it calls "std::return_slot" to get the
address, and we use "construct_at":

    void Func(void)
    {
        mutex *const p = std::construct_at<mutex>( std::return_slot() );
        try
        {
            p->lock();
        }
        catch(...)
        {
            p->~mutex();
            throw;
        }
    }

And then the last thing we do is use "std::nrvo" to turn it into the
function we want. "std::nrvo" is as follows:

    template<typename R, typename... Params>
    consteval auto nrvo( void(*const arg)(Params...) ) -> R(*)(Params...);

And you use it as follows:

    constexpr Func2 = std::nrvo(Func);

And then you can do:

    int main(void)
    {
        mutex m = Func2();
        m.unlock();
    }

So that would give us access to the Return Slot without requiring a
change to the core language.

Received on 2024-05-27 09:43:47