我想我的港口项目从Visual Studio 2010到Visual Studio 2012年在我的代码问题时,我有一些文件处理,看起来像这样:C++ 11个从Visual Studio 2010移动到2012
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__);
}
此建有没有问题,2010年,但在2012年,在有条件的失败:
错误C2678:“!”二进制:没有找到操作符,该操作符需要类型>'std :: unique_ptr < _Ty,_Dx>'(或者没有可接受的转换)
...
可能是'内置的C++操作符! bool)'
C++ 11标准指定unique_ptr has a bool operator允许您像上面那样进行快速检查。但陌生人,VS2012的的unique_ptr定义有这个非常操作:
_OPERATOR_BOOL() const _NOEXCEPT
{ // test for non-null pointer
return (this->_Myptr != pointer() ? _CONVERTIBLE_TO_TRUE : 0);
}
但是我在编译时得到这个错误。为什么?
是的,我可以使用ofstream
来代替,但那不是重点。
这似乎是通过使用lambda作为一个定制删除引起的。如果你使用别的东西,例如一个规则的函数对象,那么就没有问题了。 –