One approach:
Add a constructor that accepts void* .
Since the literal 0 implicitly converted to the null pointer void* , and the literal 1 isn`t, this will give the desired behavior. For security, you can argue that the pointer has a null value in ctor.
The downside is that now your class is constructive from nothing implicitly convertible to void * . Some unexpected things are so convertible - for example, before C ++ 11, std::stringstream was converted to void* , basically how to hack, because an explicit operator bool did not exist yet.
But this can work great in your project if you are aware of potential pitfalls.
Edit:
Actually, I remembered how to make it safer. Instead of void* use a pointer to a private type.
It might look like this:
class Flags { private: struct dummy {}; public: Flags (dummy* d) { ... } ... };
Literal conversion 0 will still work, and for some user-defined type will be unpredictably inadvertently converted to Flags::dummy * .
source share