c++ - 在 C++ 中使用 lambda 时出现奇怪的编译器消息

标签 c++ visual-studio-2010 lambda

所以我在 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/

相关文章:

c++ - 对 `std::thread::_M_start_thread CMake 的基准 undefined reference

c++ - 相同的输入但不同的方式输出不同的结果?

c++ - 如何在 C++ 中声明一个 Vector 对象?

c++ - 指针 vector 中的内存泄漏

c# - 创建、组合和缓存 lambda 表达式

c++ - 没有 Y Combinator 的递归 lambda 回调

c# - 如何将 WebGrid 列的 int 值转换为字符串?

c++ - 发布版本中运行时间过长(调试版本正常)

visual-studio-2010 - 从托管代码中包含时未定义 shared_ptr

android - 使用 Mono for Android 以编程方式/动态添加按钮控件以查看{示例}