explicit operator bool (and only explicit operator bool ) has a special language that allows in certain circumstances to implicitly convert it to bool . The specification language for this conversion is "context-converted to bool ".
These are the places where the language performs logical testing. The conditional expression used by if/while/for is "context converted to bool ". Like logical operators and conditional operator ( ?: .
So, while you cannot do this:
bool b = std::getline(fin, str); void func(bool) {} func(std::getline(fin, str));
You can do it:
while(std::getline(fin, str)) {...} for(;std::getline(fin, str);) {...} if(std::getline(fin, str) && somethingElse) {...}
Nicol bolas
source share