How can you modify an object without calling member functions?

后端 未结 7 2448
不知归路
不知归路 2021-02-13 15:41

At 3.10/10, the standard says:

An lvalue for an object is necessary in order to modify the object except that an rvalue of class type can also be used to

7条回答
  •  野趣味
    野趣味 (楼主)
    2021-02-13 16:29

    Doing an implicit cast is sort of like calling a member function -- also modifying rvalue refs seems to work.

    Tested the following in vc++10 and g++ 4.4.

    struct b { int i; b(int x) : i(x) {} };
    struct a { int i; a() : i(0) { } operator b() { return i++ /* this works */, b(i); } };
    a f(a&& x) { return x.i++ /* this works */, x; }
    int main() { b b = f(a()); /* implicit operator b() cast; b.i will equal 2 */ }
    

提交回复
热议问题