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.