What standard clause mandates this lvalue-to-rvalue conversion?

后端 未结 5 2028
独厮守ぢ
独厮守ぢ 2020-12-03 16:00

Given:

int main() {
   int x = 0;
   int y = x; // <---
}

Could someone please tell me which clause of the standard (2003 preferred) man

5条回答
  •  情话喂你
    2020-12-03 16:12

    The initializer has the follwing grammar:

    initializer:
            = initializer-clause
            ( expression-list )
    
    initializer-clause:
        assignment-expression
        { initializer-list ,opt }
        { }
    

    In your example, x is an assignment-expression which follows this chain of grammar productions:

    conditional-expression  ==>
        logical-or-expression ==>
            logical-and-expression  ==>
                inclusive-or-expression  ==>
                    exclusive-or-expression  ==>
                        and-expression  ==>
                            equality-expression  ==>
                                relational-expression  ==>
                                    shift-expression  ==>
                                        additive-expression  ==>
                                            multiplicative-expression  ==>
                                                pm-expression  ==>
                                                    cast-expression  ==>
                                                        unary-expression  ==>
                                                            postfix-expression  ==>
                                                                primary-expression  ==> 
                                                                    id-expression  ==>
                                                                        unqualified-id  ==>
                                                                            identifier
    

    And an identifier "is an lvalue if the entity is a function or variable" (5.1/4 "Primary expressions").

    So in your example, the expression to the right of the = is an expression that happens to be an lvalue. It could be an rvalue of course, but it doesn't have to be. And there is no mandated lvalue-to-rvalue conversion.

    I'm not sure what the value in knowing this is, though.

提交回复
热议问题