Menu

remove the operator= check that enforce that a non-const reference is returned?

2019-04-29
2019-05-02
  • Daniel Marjamäki

    I am not sure if I like this check. From a safety perspective it's better to return a const reference. The code you can write with a non-const reference is just weird.

    What do you think.. should we remove this? Or move it to some addon perhaps?

     
  • Paul Fultz

    Paul Fultz - 2019-05-02

    The requirement for CopyAssignable require it to be a non-const reference. This could cause compile-errors when using this object with C++ algorithms since the standard algorithms can rely on this. Or this could compile but it would cause extra copies which can degrade performance.

    Furthermore, with C++20 concepts, these requirements will be checked at compile-time when calling std:: functions in the future(using std::Assignable. So if your class' assignment operator returns a const-reference then you won't be able to use it at all with C++'s algorithms whether or not the implementation relies directly on the non-const reference.

     
  • Daniel Marjamäki

    ok you have convinced me that we should keep this check.

     

Log in to post a comment.

Want the latest updates on software, tech news, and AI?
Get latest updates about software, tech news, and AI from SourceForge directly in your inbox once a month.