std::binding 到 lambda:编译错误

std::binding to a lambda: compilation error

我 运行 尝试 std::bind 到 lambda 时出错。以下代码示例可以正常编译:

#include <functional>
#include <iostream>

int main()
{
    const auto lambda1 = [](int a, int b){ return a + b; };

    std::cout << (std::bind( lambda1, std::placeholders::_1, 2))(2)
              << std::endl;

    return 0;
}

但以下不是:

#include <functional>
#include <iostream>

int main()
{
    const auto lambda2 { [](int a, int b){ return a + b; } }; // Note the "{ }-initialization"

    std::cout << (std::bind( lambda2, std::placeholders::_1, 2))(2)
              << std::endl;

    return 0;
}

这是我使用 Visual Studio 2013,更新 4 得到的错误输出:

1>------ Build started: Project: Project1, Configuration: Debug Win32 ------
1>  main.cpp
1>C:\Program Files (x86)\Microsoft Visual Studio 12.0\VC\include\xrefwrap(58): error C2064: term does not evaluate to a function taking 2 arguments
1>          C:\Program Files (x86)\Microsoft Visual Studio 12.0\VC\include\xrefwrap(118) : see reference to class template instantiation 'std::_Result_of<_Fty,int &,int &>' being compiled
1>          with
1>          [
1>              _Fty=std::initializer_list<main::<lambda_55c00b1d5f4fcd1ad46b46ad921a2385>>
1>          ]
1>          C:\Program Files (x86)\Microsoft Visual Studio 12.0\VC\include\functional(975) : see reference to class template instantiation 'std::result_of<_Funx (int &,int &)>' being compiled
1>          with
1>          [
1>              _Funx=std::initializer_list<main::<lambda_55c00b1d5f4fcd1ad46b46ad921a2385>>
1>          ]
1>          main.cpp(23) : see reference to class template instantiation 'std::_Do_call_ret<false,_Ret,std::initializer_list<main::<lambda_55c00b1d5f4fcd1ad46b46ad921a2385>>,std::tuple<std::_Ph<1>,int>,std::tuple<int &>,std::_Arg_idx<0,1>>' being compiled
1>          with
1>          [
1>              _Ret=void
1>          ]
========== Build: 0 succeeded, 1 failed, 0 up-to-date, 0 skipped ==========

这是怎么回事?初始化列表似乎有点混乱,但我不确定为什么。

之前,auto foo{x}; 创建一个类型为 x 的元素的初始化列表。

const auto lambda2 { [](int a, int b){ return a + b; } };

所以这个 lambda2 不是 lambda,它是 lambda 的初始化列表。

这已在 中修复,可能会作为缺陷追溯应用。很意外。