A standard idiom is
while(std::getline(ifstream, str))
...
So if that works, why can't I say
bool getval(std::string &val)
{
...
std::ifstream infile(filename);
...
return std::getline(infile, val);
}
g++ says "cannot convert 'std::basic_istream<char>' to 'bool' in return
".
Is the Boolean context of a return
statement in a bool
-valued function somehow different from the Boolean context of while()
, such that the magic conversion that std::basic_istream
performs in one context doesn't work in the other?
Addendum: There's apparently some version and perhaps language standard dependency here. I got the aforementioned error with g++ 8.3.0. But I don't get it with gcc 4.6.3, or LLVM (clang) 9.0.0.
return !!std::getline(infile, val);
would be an option – Uraeusreturn std::getline(infile, val).good();
– Sporule