If "use GCC with -std=c++11 or -std=c++14 it may come -std=c++14 :
error: cannot convert 'std::istream {aka std::basic_istream<char>}' to 'bool'
Why? The C ++ 11 standard made the bool operator explicit ( ref ). Therefore, you must use:
std::ifstream ifs(filename); int a, b; if (!std::static_cast<bool>(ifs >> a >> b)) cerr << "failed";
Personally, I prefer to use the fail function below:
std::ifstream ifs(filename); int a, b; ifs >> a >> b if (ifs.fail()) cerr << "failed";
source share