Lvalue to rvalue conversion not performed
Asked Answered
T

2

2

The following function returns an rvalue:

int foo()
{
    int x = 42;
    return x;    // x is converted to prvalue
}

Clang's AST also shows the conversion:

`-FunctionDecl <line:1:1, line:5:1> line:1:5 foo 'int ()'
  `-CompoundStmt <line:2:1, line:5:1>
    |-DeclStmt <line:3:5, col:15>
    | `-VarDecl <col:5, col:13> col:9 used x 'int' cinit
    |   `-IntegerLiteral <col:13> 'int' 42
    `-ReturnStmt <line:4:5, col:12>
      `-ImplicitCastExpr <col:12> 'int' <LValueToRValue>
                                         ^^^^^^^^^^^^^^
        `-DeclRefExpr <col:12> 'int' lvalue Var 0x627a6e0 'x' 'int'

The following also performs an lvalue to rvalue conversion, this time for the parameter going into the function.

void f(int i) {}
int main()
{
    int x{3};
    f(x);
}

The AST includes the conversion:

`-FunctionDecl <line:2:1, line:6:1> line:2:5 main 'int ()'
  `-CompoundStmt <line:3:1, line:6:1>
    |-DeclStmt <line:4:5, col:13>
    | `-VarDecl <col:5, col:12> col:9 used x 'int' listinit
    |   `-InitListExpr <col:10, col:12> 'int'
    |     `-IntegerLiteral <col:11> 'int' 3
    `-CallExpr <line:5:5, col:8> 'void'
      |-ImplicitCastExpr <col:5> 'void (*)(int)' <FunctionToPointerDecay>
      | `-DeclRefExpr <col:5> 'void (int)' lvalue Function 0x6013660 'f' 'void (int)'
      `-ImplicitCastExpr <col:7> 'int' <LValueToRValue>
                                        ^^^^^^^^^^^^^^
        `-DeclRefExpr <col:7> 'int' lvalue Var 0x60138a0 'x' 'int'

As I understand it, in the same way, the following should also require an lvalue to rvalue conversion.

struct A{};
void f(A a) {}
int main()
{
    A a;
    f(a);
}

But it never shows up in the AST:

`-CallExpr <line:6:5, col:8> 'void'
  |-ImplicitCastExpr <col:5> 'void (*)(A)' <FunctionToPointerDecay>
  | `-DeclRefExpr <col:5> 'void (A)' lvalue Function 0x615e830 'f' 'void (A)'
  `-CXXConstructExpr <col:7> 'A' 'void (const A &) noexcept'
    `-ImplicitCastExpr <col:7> 'const A' lvalue <NoOp>
      `-DeclRefExpr <col:7> 'A' lvalue Var 0x615ea68 'a' 'A'

Why? Is the conversion optional sometimes?

Turret answered 25/4, 2018 at 14:19 Comment(0)
E
4

Why? Is the conversion optional sometimes?

It's not needed, and suppressed.

For the class type A, f(a); causes the copy constructor of A to be invoked. The implicitly defined copy constructor takes an lvalue reference (i.e. const A&), and lvalue-to-rvalue conversion is suppressed when binding lvalue-reference.

[dcl.init.ref]/5.1:

(5.1) If the reference is an lvalue reference ...

...

[ Note: The usual lvalue-to-rvalue, array-to-pointer, and function-to-pointer standard conversions are not needed, and therefore are suppressed, when such direct bindings to lvalues are done. — end note ]

Eichhorn answered 25/4, 2018 at 14:56 Comment(0)
F
1

The AST shows that a constructor for A is parsed, namely one that takes the lvalue const A& (the a in main) and constructs the a in f(A a). There is no rvalue here.

`-CallExpr <line:6:5, col:8> 'void'
  |-ImplicitCastExpr <col:5> 'void (*)(A)' <FunctionToPointerDecay>
  | `-DeclRefExpr <col:5> 'void (A)' lvalue Function 0x615e830 'f' 'void (A)'
  `-CXXConstructExpr <col:7> 'A' 'void (const A &) noexcept'
    ^^^^^^^^^^^^^^^^                    ^^^^^^^^^
    `-ImplicitCastExpr <col:7> 'const A' lvalue <NoOp>
      `-DeclRefExpr <col:7> 'A' lvalue Var 0x615ea68 'a' 'A'
Flexor answered 25/4, 2018 at 14:29 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.