Posted by: rmn on: 11/10/2009
Both std::cin and std::cout provide a conversion to a void* type, but not to a boolean:
operator void* (); // zero if and only if fail()
What use does a conversion to a boolean have, and which pitfalls does the current implementation save us from?
First off, a conversion to a boolean type is useful for the following construct:
while (cin >> str) // ...
But why is it a conversion to a void* and not to a bool?
This is actually pretty clever. If there was a conversion to a bool type, the following lines would not generate a compile error:
int n = 3; cin << n; // convert cin to bool, promote to int & shiftleft cout >> n; // convert cout to bool, promote to int & shiftright
This is a pretty good example of a smart design by the STL folks.
But in the std namespace there is an overload of the operator<<(ostream& os, int) why would the compiler choose to convert cin to bool rather then calling operator<<(..) with the given paramters?
please answer this one.
14/10/2009 at 23:18
hey!
It was very interesting!
Thank’s for the info!
(I love the way you’ve written, it was fascinating!)