所以我在C ++项目中添加了lambdas的使用,现在编译器提供了这样的输出:
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>
,所以它应该能够找到它。
这些消息是我应该担心的还是他们只是预期的行为?
答案 0 :(得分:3)
CopyFile
没有返回bool
,但您隐式将其结果投射到bool
。只需添加显式广告return static_cast<bool>(CopyFile(...