Skip to content

[expr.call]/9 The result of a function call may not be the same as the result of the operand of its return statement #3124

Closed
@sdkrystian

Description

@sdkrystian

[expr.call] p9 states:

The result of a function call is the result of the operand of the evaluated return statement in the called function [...]

However, this may not always work. For example:

int a = 0;
int f() 
{
  return a; // result of the operand a is an object
}
int b = f(); // function call is a prvalue, result should be a value, not an object

Additionally, if the result of the function is the result of the operand, then the wording in [stmt.return] p2 sentence 4 would have the operand initialize itself in the case of the function call and the operand being glvalues (the result of the function would be the result of the operand which would be the objects it denotes, so initializing the result of the function would initialize that object). Even crazier, in the case of the operand being a prvalue, this would have the operand initializing a value (result of the function would be the value of the operand).

To remedy this, it could be specified that the result of the function is the operand after any conversions are applied to the operand to convert it to the type and value category of the function call, but this could introduce some problems for prvalues, as they should not be materialized, and instead used to initialize the object itself.

Alternatively, the wording in [stmt.return] p2 sentence 4 could be removed entirely, and instead, [expr.call] p9 could be written to say:

If a function call is a prvalue, the result object is copy-initialized with the operand of the return statement. Otherwise, the glvalue result is the object denoted by t in some invented declaration T t = e; [Note: the lifetime of a temporary bound to this invented declaration is dictated by the lifetime rules for a temporary bound to a returned value of a function - end note] where e is the operand of the evaluated return statement and T is the return type of the function.

And this would solve the problem, as if the function call is a prvalue, the result would be its value, and if the function call is a glvalue, the result would simply be the object that the operand denoted, or the temporary that results from initializing the result.

Metadata

Metadata

Assignees

Labels

cwgIssue must be reviewed by CWG.

Type

No type

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions