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?