Has anyone found the need to declare the return parameter of a copy assignment operator const?

前端 未结 7 2067
误落风尘
误落风尘 2020-12-14 12:02

The copy assignment operator has the usual signature:

    my_class & operator = (my_class const & rhs);

Does the following signatur

7条回答
  •  佛祖请我去吃肉
    2020-12-14 12:46

    Why is everyone obsessing over (a = b) = c? Has that ever been written by accident?

    There is probably some unforeseen utility of the result of assignment being altered. You don't just make arbitrary rules against made-up examples that look funny. Semantically there is no reason that it should be const, so do not declare it const for lexical side effects.

    Here is an example of somewhat reasonable code that breaks for const & assignment:

    my_class &ref = a = b;
    

提交回复
热议问题