Git Product home page Git Product logo

Comments (6)

colonelsammy avatar colonelsammy commented on August 27, 2024

That's an interesting one! I hadn't thought of a non-const member function . . .

I think I'm with you on this one - if you must have one, then the canonical form of a member op==() is 'const'.

Perhaps you could capture the non-const case and issue a more sensible compile time error ?

from catch2.

philsquared avatar philsquared commented on August 27, 2024

@colonelsammy I could possibly force a descriptive error message - but it would still be limiting your options - what if you don't control the source?
I think providing a non-const operator ==, or an operator == that takes its argument by non-const ref, are the lesser two of four evils (when you add in providing const and non-const overloads and making them do something different). After all the first two are probably just an oversight. The other two are deliberately malicious!
That's why I'm leaning towards supporting the former two at the expense of the latter two.

from catch2.

jalfd avatar jalfd commented on August 27, 2024

About limiting your options, remember that the operator== can be defined outside the class, so even if you can't modify the class in question, you can always define a non-member const operator==. It's up to you to decide if it's reasonable to put this burden on the library user though. ;)

(But it does seem like a very rare case, and if it saves you a lot of trouble, it may be worth it)

from catch2.

philsquared avatar philsquared commented on August 27, 2024

Thanks for the comments.
I've gone with the const_cast approach for now but I may revisit this in the future.

from catch2.

JoeHCQ1 avatar JoeHCQ1 commented on August 27, 2024

Reading through the comments, I'm having a hard time tracking exactly what is and isn't supported.

I believe these are the four options - which ones are supported? I'm trying to figure out if my current experience is the expected behavior or not.

MyClass {
  public:
   bool operator==(MyClass &rhs); /* supported ? */
   bool operator==(const MyClass &rhs); /* supported ? */
   bool operator==(MyClass &rhs) const; /* supported ? */
   bool operator==(const MyClass &rhs) const; /* supported ? */
};

from catch2.

horenmar avatar horenmar commented on August 27, 2024

Only the last one is.

from catch2.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.