I am trying to migrate my project from Visual Studio 2010 to Visual Studio 2012. In my code, I have file processing that looks like this:
auto fileDeleter = [](FILE* f) { fclose(f); }; unique_ptr<FILE, decltype(fileDeleter)> fMinute( fopen(minuteLogName.c_str(), "w"), fileDeleter); unique_ptr<FILE, decltype(fileDeleter)> fIndividual( fopen(individualLogName.c_str(), "w"), fileDeleter); if (!fMinute || !fIndividual) { throw Exceptions::IOException("One of the log files failed to open", __FUNCTION__); }
It was built without problems in 2010, but in 2012 it fails on condition:
error C2678: binary '!': operator not found that accepts the left operand of type> 'std :: unique_ptr <_Ty, _Dx>' (or there is no acceptable conversion)
...
may be a "built-in C ++ operator! (bool)"
The C ++ 11 standard states that unique_ptr has a bool operator so that you can perform quick checks like above. Even stranger, the definition of VS2012 unique_ptr has this same operator:
_OPERATOR_BOOL() const _NOEXCEPT { // test for non-null pointer return (this->_Myptr != pointer() ? _CONVERTIBLE_TO_TRUE : 0); }
But I get this error when compiling. Why?
Yes, I could just use ofstream instead, but that's beyond the point.
Matt kline
source share