Why does operator ++ return a non-const value?

后端 未结 3 712
不思量自难忘°
不思量自难忘° 2020-12-30 23:17

I have read Effective C++ 3rd Edition written by Scott Meyers.

Item 3 of the book, \"Use const whenever possible\", says if we want to prevent rvalues f

3条回答
  •  梦谈多话
    2020-12-30 23:55

    If it is non-const, I expect *(++it) to give me mutable access to the thing it represents.

    However, dereferencing a const iterator yields only non-mutable access to the thing it represents. [edit: no, this is wrong too. I really give up now!]

    This is the only reason I can think of.

    As you rightly point out, the following is ill-formed because ++ on a primitive yields an rvalue (which can't be on the LHS):

    int* p = 0;
    (p++)++;
    

    So there does seem to be something of an inconsistency in the language here.

提交回复
热议问题