Lvalue to rvalue conversion not performed

☆樱花仙子☆ 提交于 2020-01-04 06:01:04

问题


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?


回答1:


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 ]




回答2:


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'


来源:https://stackoverflow.com/questions/50024666/lvalue-to-rvalue-conversion-not-performed

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!