The standard ios class overrides the void * operator such that it can be used in conditionals.
if (std::cin) { ... }
I have a class whose function returns a status.
Status DoSomething()
It would be good to be able to use Status in an if-statement if the use doesn’t need a fine-grained return status.
if (DoSomething()) { ... } // just want to know if pass or fail
// or if I need more info
Status s = DoSomething()
switch (s) { ... }
Is the ios trick good for this use case or not? Is it even a good idiom in general?
No. In C++03 you should instead use safe-bool idiom. In C++11, you should use
explicit operator bool. Never useoperator void*to do bool conversion. They have to be defined onStatustype, obviously.Note that this doesn’t affect
switch— to be able to use that, you need to have a conversion operator to an integer or an enumeration.