Consider the following code:
std::ostream file;
if( file == NULL ) std::cout << "Failed to open file" << std::endl;
It compiles perfectly when passing -std=gnu11 (default from GCC 5.2), or just usinggcc code.cpp -o a.out
.
It fails with -std=gnu++11, though:
no match for ‘operator==’ (operand types are ‘std::ofstream {aka std::basic_ofstream<char>}’ and ‘long int’)`
What is the simplest workaround?
Details:
I've got to use std=gnu++11 to have access to shared_ptr
definitions. Furthermore, some code of mine is automatically generated and modifying the generator would incur in a reasonable effort - so I was wondering if someone could come up with simpler way to get rid of this "lack of compatibility".
The only reason why this ever compiled in the first place is because std::ios
, which ofstream
derives from, provided a non-explicit(!) operator void*
prior to C++11. As of C++11, explicit operator bool
is provided instead, which doesn't allow for implicit conversions as necessitated by your code. Instead, write
if (!file) std::cout << "Failed to open file" << std::endl;
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With