3

Horstmann’s C++ pitfalls tackles an interesting point when talking about streams. To quote him:

Use conversion to void*, not conversion to int or bool, to implement objects yielding truth values. Unlike int or bool, 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?

casperOne
  • 73,706
  • 19
  • 184
  • 253
qdii
  • 12,505
  • 10
  • 59
  • 116
  • 1
    It's advisable when is a good idea, and it is inadvisable if it is a bad idea. What sort of answer did you expect? – Kerrek SB Feb 03 '12 at 20:15
  • @KerrekSB: cases, examples, to be able to distinguish between the good practices and the bad practices, I’m a bit confused right now. Why don’t we always do that? Why don’t we just `typedef void* secure_bool` – qdii Feb 03 '12 at 20:17
  • 2
    Relevant: http://stackoverflow.com/questions/6242296/conversion-function-for-error-checking-considered-good and http://stackoverflow.com/questions/8809224/how-does-this-conversion-to-bool-work and http://stackoverflow.com/q/6242768/500104 (and here with examples: http://www.artima.com/cppsource/safebool.html, also linked) – R. Martinho Fernandes Feb 03 '12 at 20:17
  • Horstmann is talking about conversion operators, not return types. Your question makes no sense. – ildjarn Feb 03 '12 at 20:23
  • @ildjarn : The statement `Unlike int or bool, void* have no legal operations other than == comparison` is what I’m referring to, regardless of conversions. – qdii Feb 03 '12 at 20:25
  • To the downvoter: please state your point, this is how we all progress ain’t it? – qdii Feb 03 '12 at 20:28
  • I'm the downvoter, and I _did_ state my point. Horstmann is talking about giving classes implicit conversion operators (specifically `bool` vs. some pointer type), and you seem to be asking about arbitrary function return types. This makes no sense. – ildjarn Feb 03 '12 at 20:31
  • @ildjarn : There were 2 down votes. I was wondering about the other one. Now maybe my question was not perfectly well-stated by “is using void* instead of bool an advisable practice?” but it made sense to me.I should rephrase it with: “Horstmann shows an example where using a void* instead of a bool seems appropriate. Is it always advisable?”. – qdii Feb 03 '12 at 20:40

1 Answers1

10

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 like

std::ifstream myStream;
if (myStream) {

}
if (!myStream) {

}

Here, a conversion to bool seems more reasonable, but leads to weirdnesses like this:

if (myStream == true) // ??

The conversion to void* prevents this code from being legal, but opens up a whole other can of worms, like

delete myStream; // ??

In C++11, with the ability to have explicit operator bool() as a member function, this void* hack is deprecated and should not be used. Don't use this idiom. If you need something to return a bool, have it return a bool. If you need an object that can be converted to a bool, use explicit operator bool.

Hope this helps!

templatetypedef
  • 362,284
  • 104
  • 897
  • 1,065
  • Has the definition of `istream` changed in C++11 to use this new feature? – Kerrek SB Feb 03 '12 at 20:27
  • 2
    @Kerrek: yes. It's [a breaking change](http://stackoverflow.com/questions/6399615/what-breaking-changes-are-introduced-in-c11), but one that only breaks broken code (`delete std::cin` won't compile anymore). – R. Martinho Fernandes Feb 03 '12 at 20:28