Horstmann’s C++ pitfalls tackles an interesting point when talking about streams. To quote him:
Use conversion to
void*, not conversion tointorbool, to implement objects yielding truth values. Unlikeintorbool,void*have no legal operations other than==comparison.
As a programmer, I would be puzzled if some function returned void* when I expect a boolean. Horstmann provides an example where using a void* instead of a bool seems appropriate. Is it always advisable?
This is not advised in general circumstances and, with C++11, is not advised at all.
The reason for the conversion to
void*was to support syntax likeHere, a conversion to
boolseems more reasonable, but leads to weirdnesses like this:The conversion to
void*prevents this code from being legal, but opens up a whole other can of worms, likeIn C++11, with the ability to have
explicit operator bool()as a member function, thisvoid*hack is deprecated and should not be used. Don’t use this idiom. If you need something to return abool, have it return abool. If you need an object that can be converted to abool, useexplicit operator bool.Hope this helps!