How do iostream references evaluate to true/false
Example code:
#include <iostream>
#include <string>
#include <sstream>
using namespace std;
i开发者_高级运维nt main() {
int num;
stringstream ss("1");
if (ss >> num) cout << "It was true\n";
return 0;
}
How is "ss >> num" able to evaluate to a boolean value? Is there some operator that's been overloaded in stringstream? The STL says that the >> operator returns a reference to a stream.
Yes- the iostreams have an operator void*
in C++03 and an explicit conversion to bool
in C++11.
精彩评论