rzlwatboa
rzlwatboa

Reputation: 31

Problems with nested lambdas in VC++

Does anyone know why this code is not compilable with VC++ 2010

class C
{
public:
    void M(string t) {}
    void M(function<string()> func) {}
};

void TestMethod(function<void()> func) {}

void TestMethod2()    
{
    TestMethod([] () {
        C c;            
        c.M([] () -> string { // compiler error C2668 ('function' : ambiguous call to overloaded function)

             return ("txt");
        });
    });
}

Update:

Full code example:

#include <functional>
#include <memory>
using namespace std;

class C
{
public:
  void M(string t) {}
  void M(function<string()> func) {}
};

void TestMethod(function<void()> func) {}

int _tmain(int argc, _TCHAR* argv[])
{
   TestMethod([] () {
      C c;
      c.M([] () -> string { // compiler erorr C2668 ('function' : ambiguous call to overloaded function M)
          return ("txt");
      });
    });
    return 0;
}

Upvotes: 2

Views: 213

Answers (2)

Sebastian Mach
Sebastian Mach

Reputation: 39089

You did not post the error message, so by looking into my crystal ball I can only conclude that you suffer those problems:

Missing #includes

You need at the top

#include <string>
#include <functional>

Missing name qualifications

You either need to add

using namespace std;

or

using std::string; using std::function;

or std::function ... std::string ...

Missing function main()

int main() {}

Works with g++

foo@bar: $ cat nested-lambda.cc

#include <string>
#include <functional>

class C
{
public:
    void M(std::string t) {}
    void M(std::function<std::string()> func) {}
};

void TestMethod(std::function<void()> func) {}

void TestMethod2()    
{
    TestMethod([] () {
        C c;            
        c.M([] () -> std::string { // compiler error C2668 
             return ("txt");
        });
    });
}

int main() {
}

foo@bar: $ g++ -std=c++0x nested-lambda.cc

Works fine.

Upvotes: 0

Related Questions