C++ Type and Value Category for Expression and Variable

前端 未结 3 527
死守一世寂寞
死守一世寂寞 2021-02-06 01:31

From this link, it says that

Objects, references, functions including function template specializations, and expressions have a property called type

<
3条回答
  •  滥情空心
    2021-02-06 02:30

    It confused me first too but let me clear out the ambiguity in a simple way.

    EXPRESSION is something that can be evaluated and must evaluate to a non-reference type right ?
    ( Yes, of course duh !! )

    Now we also know a variable name is an l-value EXPRESSION.
    ( Dude get to the point already and stop making the expression word bold )

    Okay now here is the catch, when we say a variable we mean a place in memory. Now will we call a place in memory an expression ? No, definitely not that is just completely absurd.

    Expression is a generic term that we identify by defining some rules and anything that fall under those rules is an expression. It is necessary to define it this way to make sense of the code during compiler construction. One of that rule is that anything that evaluates to a value is an expression. Since from coding perspective using a variable name means that you wish the actual value is used when the code is compiled, so we call that variable name an expression.

    So when they say a variable is an expression they don't mean the variable as in place in memory but the variable NAME from coding perspective. But using the term "variable name" to differentiate from the actual variable (place in memory) is just absurd. So saying "variable is an expression" is just fine as long as you think it from coding perspective.

    Now to answer this first :

    More specifically, when a variable name is used in function call:

    SomeFunc(rf_int);
    

    Is rf_int now considered an expression (thus it is an lvalue with type int), or a variable (thus it is an lvalue with type rvalue reference to int)?

    A single variable is also an expression. So this question becomes invalid.

    Now to answer this :

    Based on the above two statement, rf_int can be treated as an expression and expression has non-reference type.

    Now I am really confused. Does rf_int have a reference type or not?

    What if I say rf_int is an r-value reference and rf_int is also an l-value EXPRESSION.
    ( Oh brother, this guy and his obsession with expression )

    It is true because if you do the following it will work.

    int &&rf_int = 10;   // rf_int is an r-value reference 
    int &x = rf_int;     // x is an l-value reference and l-value reference can be initialized with l-value expression
    cout << x;           //Output will be 10
    

    Now is rf_int an expression or a r-value reference, what will it be ? The answer is both. It depends on from which perspective you are thinking.

    In other words what I'm trying to say is that if we think rf_int as a variable (some place in memory) then surely it has the type of r-value reference but since rf_int is also a variable name and from coding perspective it is an expression and more precisely an l-value expression and whenever you use this variable for the sake of evaluation you will get the value 10 which is an int so we are bound to say that rf_int type as an expression is an int which is a non-reference type.

    If you think from compiler perceptive for a moment what line of code evaluates to a reference ? None right ?. You can try searching if you find any do let me know as well. But the point here is that the type of expression doesn't mean type of variable. It means the type of value that you get after evaluating the expression.

    Hope I have clarified your question. If I missed something do let me know.

提交回复
热议问题