visual-c++lambda

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

Solution

  • This is a bug of the VC++ Compiler.

    https://connect.microsoft.com/VisualStudio/feedback/details/687935/ambiguous-call-to-overloaded-function-in-c-when-using-lambdas