Skip to content

The operand in the copy-initialization of the result object of a function should be the possibly-converted one #4847

Closed
@xmh0511

Description

@xmh0511

[stmt.return] p2 says

the return statement initializes the glvalue result or prvalue result object of the (explicit or implicit) function call by copy-initialization from the operand.

where the operand is defined as

The expr-or-braced-init-list of a return statement is called its operand.

Instead, [expr.call] p9 says

The result of a function call is the result of the possibly-converted operand of the return statement ([stmt.return]) that transferred control out of the called function (if any)

It seems to be a bit inconsistent. It is not clear whether the return statement uses the possible-converted operand to copy-initialize the result object or just the original operand. The concern comes from the following example

struct B{
  B(int){}
};
struct A{
  operator B(){
     return 0;
  }
};
A a;
B const& rf = a;  //#1

According to [dcl.init.ref#5.4.1], the initializer expression at #1 should be first converted to type B with a user-defined conversion, which is B::operator B, and [dcl.init.ref#5.4.1] also says

The result of the call to the conversion function, as described for the non-reference copy-initialization, is then used to direct-initialize the reference. For this direct-initialization, user-defined conversions are not considered.

For the direct-initialization of the reference, temporary materialization conversion will be applied to the conversion function call, which means the temporary object is used as the result object that is copy-initialization from the operand of the return statement, as aforementioned. At this point, according to [dcl.init#general-15.6.3], it will invoke user-defined conversions to transform the operand (0) to the destination type to initialize the result object(temporary object ). However, as [dcl.init.ref#5.4.1] said, user-defined conversions are not considered.

I think the wording about the return statement may be that

the return statement initializes the prvalue result object of the (explicit or implicit) function call by copy-initialization from the possibly-converted operand that is implicitly converted to the return type(if any).

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions