make fails with error "cannot convert ‘std::istream {aka std::basic_istream<char>}’ to ‘bool’ in return"

Steffan Daniel Bos-Haugen picture Steffan Daniel Bos-Haugen · Jul 29, 2016 · Viewed 8.7k times · Source

I'm trying to compile libgtextutils (required by the fastxtoolkit) from source. The './configure' command runs nicely, however subsequent 'make' command produces an error that I cannot resolve.

text_line_reader.cpp: In member function ‘bool TextLineReader::next_line()’:
text_line_reader.cpp:47:9: error: cannot convert ‘std::istream {aka std::basic_istream<char>}’ to ‘bool’ in return
  return input_stream ;
         ^~~~~~~~~~~~
make[3]: *** [text_line_reader.lo] Error 1
make[2]: *** [all-recursive] Error 1
make[1]: *** [all-recursive] Error 1
make: *** [all] Error 2

I'm on a Mac, OSX 10.11.6 (Intel)

Any suggestions that might solve this are highly appreciated.

Answer

Jonathan Wakely picture Jonathan Wakely · Jul 29, 2016

See the Porting to GCC 6 guide, which documents this as one of the changes you must deal with due to GCC 6 defaulting to C++14 mode instead of C++03 mode:

Cannot convert 'std::ostream' to 'bool'

As of C++11, iostream classes are no longer implicitly convertible to void* so it is no longer valid to do something like:

bool valid(std::ostream& os) { return os; }

Such code must be changed to convert the iostream object to bool explicitly, e.g. return (bool)os; or return static_cast<bool>(os);

Another option is to explicitly use -std=c++03 in your compiler flags to compile in C++03 mode, but it's better to fix the code. The fixes given above will make the code compatible with any C++ version.