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;
}
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
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.
精彩评论