开发者

How do iostream references evaluate to true/false

开发者 https://www.devze.com 2023-03-30 17:21 出处:网络
Example code: #include <iostream> #include <string> #include <sstream> using namespace std;

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.

0

精彩评论

暂无评论...
验证码 换一张
取 消