Why pre-increment operator gives rvalue in C?

前端 未结 3 1553
春和景丽
春和景丽 2020-12-14 06:52

In C++, pre-increment operator gives lvalue because incremented object itself is returned, not a copy. But in C, it gives rvalue. Why?

3条回答
  •  我在风中等你
    2020-12-14 07:35

    C99 says in the footnote (of section $6.3.2.1),

    The name ‘‘lvalue’’ comes originally from the assignment expression E1 = E2, in which the left operand E1 is required to be a (modifiable) lvalue. It is perhaps better considered as representing an object ‘‘locator value’’. What is sometimes called ‘‘rvalue’’ is in this International Standard described as the ‘‘value of an expression’’.

    Hope that explains why ++i in C, returns rvalue.


    As for C++, I would say it depends on the object being incremented. If the object's type is some user-defined type, then it may always return lvalue. That means, you can always write i++++++++ or ++++++i if type of i is Index as defined here:

    Undefined behavior and sequence points reloaded

提交回复
热议问题