lundi 29 janvier 2018

std::binding to a lambda: compilation error

I ran into an error trying to std::bind to a lambda. The following code sample compiles fine:

#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;
}

but the following doesn't:

#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;
}

Here's the error output I got using Visual Studio 2013, update 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 ==========

What is going on? There seems to be some kind of mix up with initializer list but I'm not sure I nee why.

Aucun commentaire:

Enregistrer un commentaire