C++ Logo

std-discussion

Advanced search

Re: named function return value

From: F.v.S. <de34_at_[hidden]>
Date: Sat, 02 Apr 2022 20:43:57 +0800

Hi,

How do you think about P2025 (https://wg21.link/p2025)? IMO P2025 offers more flexible patterns.

If some explicit notions that enforces NRVO while avoiding boilerplate is wanted, I think we should add them to variable definitions rather than function declarations.

Thanks,
Jiang An
在 2022年4月2日 20:06,Замфир Йончев via Std-Discussion <std-discussion_at_[hidden]>写道:
Hi,

I'm not sure if this is the right place or the right way to ask but I was wondering if there is a (similar) proposal for the following:

Adding an explicit name and an initial value to the return value of a function.
I imagine the following function signature (in pseudocode) :

auto func_name(Type1 arg1, Type2 arg2, etc.) -> RetType ret_value_name {init_value};
 
The change from existing code is the addition of a name after the return type and an initialization.
Think of the initialization as a default value.

I've noticed a lot of cases where a programmer would write functions like this:

int vector_sum(const std::vector<int>& vec) // or auto sum(const std::vector<int>& vec) -> int
{
  int result = 0;
  for(const auto value : vec) result += value;
  return result;
}

The bolded lines are repeated in many functions which return a new object constructed or built from the function arguments.

I imagine with this new syntax these lines would be implicitly generated by the compiler.
So, the function above can be written like this:

auto vector_sum(const std::vector<int>& vec) -> int result {0}
{
  for(const auto value : vec) result += value;
}

I was inspired by Ben Deane's talk at CppCon2021 about adding an explicit type to the 'this' argument.
This is a similar idea but related to an explicit name of the return value.

Some clarifications:

As far as I can tell the initialization is required because if it's optional the compiler won't be able to tell if the curly braces belong to the initialization or the function body.
So, if the compiler sees an identifier after the return type it should always expect an initializer before the function body (or before the semicolon for function declarations).
If it doesn't see an identifier after the return type it should expect the function body straight away (how it is now).

For function declarations (without a body) if you don't need (or you cannot) provide an initial value, you should not provide an explicit return value name.
So it's either both a name and an initialization or none.
Then in the actual definition you can provide the name and the initial value.
Similarly to how currently you can omit the argument names in function declarations and then provide them in the definition.

The usage of curly braces in the initialization also doesn't cause problems with pure virtual function declarations.
For example:
virtual auto func(int arg) -> int result {10} = 0;
Although, in most cases I don't think you would need an explicit return value name in pure virtual functions.

What do you think about this idea?
Has anyone proposed something like this?

Best regards,
Zamfir Yonchev

Received on 2022-04-02 12:44:13