If I have overloaded operator bool(). Do I need to overload operator !() too? When and why. Thanks for help.
If I have overloaded operator bool() . Do I need to overload operator !()
Share
Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.
Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.
Lost your password? Please enter your email address. You will receive a link and will create a new password via email.
Please briefly explain why you feel this question should be reported.
Please briefly explain why you feel this answer should be reported.
Please briefly explain why you feel this user should be reported.
You should also implement
operator!()if you want a developer to be able to say!myobjectwheremyobjectis an instance of your class.Section 13.3.1.2 specifies that when applying a unary operator to an object of user-defined type
So the compiler may use the built-in
bool operator!(bool)and your user-defined conversion, but only when youroperator bool()is implicitly callable.operator bool()is almost always made explicit to avoid its use in arbitrary integer contexts. Multiple user-defined conversions could also create ambiguity among built-in candidate operators as chris mentioned in a comment.So it’s best to just define
operator!()yourself.