c++ – Prevent casting a pointer to an object to bool

Question:

Is it possible to prevent the compiler from casting a pointer to an object to bool?

class A {
}

A* a = nullptr;
if (a) {    // <-- как запретить?
    ...
}

Answer:

Wrap a pointer in a structure

template<typename T>
struct ptr {
  T* p;
  T& operator*();
  T* operator->();
};
Scroll to Top