C++ Logo

std-proposals

Advanced search

Re: [std-proposals] std::elide

From: Frederick Virchanza Gotham <cauldwell.thomas_at_[hidden]>
Date: Sun, 19 May 2024 12:58:37 +0100
On Sunday, May 19, 2024, Frederick Virchanza Gotham wrote:

>
> mutex Func(void)
> {
> auto &m = _Retvar(mutex);
> m.lock();
> return; // returns object created by _Retvar
> }
>


If control reaches the end of an NRVO function before an object has been
created, then we could dictate either:
    (a) undefined behaviour
    (b) an exception of type 'std::nrvo_empty_t' gets thrown

Also we could mark the definition (but not the declaration) of an NRVO
function as follows:

mutex Func(void) _Retvar
{
    // implementation
}

And so then when it comes to the return statement, we could make it simply:

    return;

or more verbosely:

    return _Retvar;




> mutex Func(void)
> {
> bool destroy = false;
> try
> {
> auto &m = _Retvar(mutex, destroy);
> m.lock();
> }
> catch(...)
> {
> if ( destroy ) m.~mutex();
> }
> return;
> }
>


I realise 'm' is out of scope here but you get the idea.

Received on 2024-05-19 11:58:39