所以我在 C++ 项目中添加了 lambda 的使用,现在编译器给出如下输出:
9>C:\Program Files (x86)\Microsoft Visual Studio 10.0\VC\include\xxcallobj(13): warning C4800: 'BOOL' : forcing value to bool 'true' or 'false' (performance warning)
3> C:\Program Files (x86)\Microsoft Visual Studio 10.0\VC\include\xxfunction(386) : see reference to class template instantiation 'std::tr1::_Impl_no_alloc0<_Callable,_Rx>' being compiled
3> with
3> [
3> _Callable=_MyWrapper,
3> _Rx=bool
3> ]
3> C:\Program Files (x86)\Microsoft Visual Studio 10.0\VC\include\xxfunction(369) : see reference to function template instantiation 'void std::tr1::_Function_impl0<_Ret>::_Reset0o<_Myimpl,_Fty,std::allocator<_Ty>>(_Fty,_Alloc)' being compiled
3> with
3> [
3> _Ret=bool,
3> _Fty=`anonymous-namespace'::<lambda3>,
3> _Ty=std::tr1::_Function_impl0<bool>,
3> _Alloc=std::allocator<std::tr1::_Function_impl0<bool>>
3> ]
3> C:\Program Files (x86)\Microsoft Visual Studio 10.0\VC\include\functional(113) : see reference to function template instantiation 'void std::tr1::_Function_impl0<_Ret>::_Reset<_Fx>(_Fty)' being compiled
3> with
3> [
3> _Ret=bool,
3> _Fx=`anonymous-namespace'::<lambda3>,
3> _Fty=`anonymous-namespace'::<lambda3>
3> ]
3> ..\..\Common\IOFile.cpp(1162) : see reference to function template instantiation 'std::tr1::function<_Fty>::function<`anonymous-namespace'::<lambda3>>(_Fx)' being compiled
3> with
3> [
3> _Fty=bool (void),
3> _Fx=`anonymous-namespace'::<lambda3>
3> ]
我的 lambda 代码是:
在.h中:
typedef std::function<bool ()> RepeatingFunction;
static bool RepeatFileOperation(RepeatingFunction callback);
static bool Copy(CString file, CString copyFileName, bool failIfExists = true);
在 .cpp 中:
bool IOFile::RepeatFileOperation(RepeatingFunction callback)
{
const int times_to_retry = 10;
bool succeed = false;
// Retry a few times if it doesn't work
int retries = 0;
do
{
// Perform the caller's action on the file
succeed = callback();
}
while (!succeed && retries++ < times_to_retry);
return succeed;
}
bool IOFile::Copy(CString file, CString copyFileName, bool failIfExists)
{
return RepeatFileOperation([&] {
return CopyFile(file, copyFileName, static_cast<BOOL>(failIfExists));
});
}
程序仍然编译得很好。我用谷歌搜索了这个错误,发现人们收到了类似的消息,但在他们的情况下,程序没有构建。在所有情况下,他们的构建错误似乎都与前向声明有关,但正如我所说,我的构建很好,我包括了 <functional>
在头文件中,因此它应该能够很好地找到它。
这些消息是我应该担心的事情还是它们只是预期的行为?
最佳答案
CopyFile
不返回 bool
,但您将其结果隐式转换为 bool
.只需添加一个显式转换 return static_cast<bool>(CopyFile(...
关于c++ - 在 C++ 中使用 lambda 时出现奇怪的编译器消息,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25444044/