C++ Logo

std-proposals

Advanced search

Re: [std-proposals] Return Value Optimisation whenever you need it (guaranteed elision)

From: Thiago Macieira <thiago_at_[hidden]>
Date: Sat, 12 Aug 2023 10:26:05 -0700
On Saturday, 12 August 2023 09:14:28 PDT Marcin Jaczewski wrote:
> What is the point of any keyword or anything else there?
> why code like:
> ```
> Mutex m;
> return m;
> ```
> could not work out of the box?

I don't know, I didn't investigate the situation. I assumed that there was a
reason, but you're showing that that assumption is incorrect.

> ```
> {
> int [[nrvo]] i = 0;
>
> if (x) return 0; //compiler error, `i` was defined `nrvo` but
> // diffrent object was returned during life time of `i`
>
> return i;
> }

This can't be an error with just a plain attribute. It can be a warning, which
in turn must mean the code is perfectly legal otherwise.

Or if it is a syntactic error (let's please stop using "int" because it is
neither unmovable nor does it have a return slot), then the attribute is
unnecessary.

It would only be useful where NVRO is an optional optimisation and you want
the compiler to warn you if it couldn't be applied. By extension, the
attribute could be applied where the named return is mandatory, but that being
the case, there would never be a warning.

> Attribute is here only to state intention and prevent errors
> (like the user wanted NRVO but the compiler could not provide it).
> This should be general rule in lange, identity of returned object
> should be always preserved (if returning thing is of course unabigius).

Agreed.

-- 
Thiago Macieira - thiago (AT) macieira.info - thiago (AT) kde.org
   Software Architect - Intel DCAI Cloud Engineering

Received on 2023-08-12 17:26:07