The code below compiles in VS 2012 but not in VS 2013
std::ofstream stm;
if(stm != NULL)
{
}
In VS 2013 you get this compilation error:
binary '!=' no operator found which takes a left-hand operand of type 'std::ofstream' (or there is no acceptable conversion)
I looked at the headers and in <xiobase>
and I found the following:
VS2012
ios_base::operator void *() const;
VS2013
operator void *() const
has been removed and the operator bool with explicit was added instead:
ios_base::explicit operator bool() const;
Now my questions:
- I couldn't find any information about this change in the internet. Do you know if there is an official article about this change anywhere?
- I have legacy code where if(stm != NULL) is used a lot. For unrelated reasons it's preferable not to change the code. Is there a way to make it compile in VS 2013 without changing it? I couldn't find any conditional compilation directives that could restore operator
void*
or removeexplicit
from operator bool().
PS: gcc 4.9.0 still has operator void*() const
. So it will not have this problem.
UPDATE:
To make my legacy code compile I implemented the following overloads as it was suggested:
#include <xiosbase>
bool operator==(const std::basic_ios<char, char_traits<char>> &stm, int null_val)
{
return static_cast<bool>(stm) == null_val;
}
bool operator==(int null_val, const std::basic_ios<char, char_traits<char>> &stm)
{
return operator==(stm, null_val);
}
bool operator!=(int null_val, const std::basic_ios<char, char_traits<char>> &stm)
{
return !operator==(stm, null_val);
}
bool operator!=(const std::basic_ios<char, char_traits<char>> &stm, int null_val)
{
return !operator==(stm, null_val);
}
In my case the char
value type was enough and the second parameter is int because something that is not NULL is not supported anyway.