6

Many of the comments on How to ask a question which is similar to other downvoted and closed questions? asked for details re: the specific question I was asking, but since that question has already been answered in a generic fashion, I felt it best to create a separate question.

The question I want to ask on SO is:

Ignoring user-defined operator overloads, are there any cases where if (!x) would behave differently from if (x == nullptr)

e.g. Are there any types in the standard library for which !x would behave differently from x == nullptr

A quick google search returns many similar questions already on SO, many of which have been closed. to name a few:

I believe my question to be different from the existing questions for the following reasons:

  • nullptr vs 0/NULL
    • there is the potential for a different sequence of type conversions. I don't know enough about implicit conversions to figure out whether this could indeed change behaviour though.
    • x == 0 (and x == NULL when NULL is not defined as nullptr) is not supported by some of the standard library types such as std::shared_ptr, but every one I checked supported x == nullptr and x != nullptr
  • I'm not interested in the obvious case of when a user defines operator overloads such that !((bool)x) and x == nullptr are different.
  • I'm not asking for personal preferences, I'm interested in whether there are any objective differences between the two choices
2
  • Off-topic, but std::optional<T*> is an answer to your question. Given std::optional<T*> x{nullptr};, !x returns false, while x == nullptr returns true.
    – L. F.
    Commented Sep 13, 2019 at 3:25
  • "Is my similar question OK to post?" [checks the language, sees it's c++] Suicide is never the right choice, sir.
    – user1228
    Commented Sep 13, 2019 at 15:39

0

You must log in to answer this question.

Browse other questions tagged .