Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is "if (getline(fin, str)) {}" conforming to the C++11 standard?

I checked the C++11 standard and found the following facts:

  1. std::getline(fin, str) returns a basic_ios object, whose class has a member function explicit operator bool() const;

  2. The class basic_ios doesn't have a member function operator void*() const; as pre-C++11.

So, I think if (getline(fin, str)) {} is not standard conforming. It should be written as

if (bool(getline(fin, str)){}. (However, VC++ 2012 gives a warning on this usage. i.e. force void* to bool)

Am I correct?

like image 483
xmllmx Avatar asked Feb 06 '13 03:02

xmllmx


1 Answers

The code is conforming. The explicit conversion operator to bool will be called when the object is used as a condition automatically. The change in the standard was meant to maintain that same usage while making it a bit safer.

like image 136
David Rodríguez - dribeas Avatar answered Oct 11 '22 17:10

David Rodríguez - dribeas