Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/gwt/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Visual c++ VC+;中嵌套lambda的问题+;_Visual C++_Compiler Construction_Lambda - Fatal编程技术网

Visual c++ VC+;中嵌套lambda的问题+;

Visual c++ VC+;中嵌套lambda的问题+;,visual-c++,compiler-construction,lambda,Visual C++,Compiler Construction,Lambda,有人知道为什么这段代码不能用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

有人知道为什么这段代码不能用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");
        });
    });
}
C类
{
公众:
空M(字符串t){}
void M(函数func){}
};
void TestMethod(函数func){}
void TestMethod2()
{
TestMethod([])(){
C C;
c、 M([]()->字符串{//编译器错误C2668('function':对重载函数的调用不明确)
返回(“txt”);
});
});
}
更新:

完整代码示例:

#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;
}
#包括
#包括
使用名称空间std;
C类
{
公众:
空M(字符串t){}
void M(函数func){}
};
void TestMethod(函数func){}
int _tmain(int argc,_TCHAR*argv[]
{
TestMethod([])(){
C C;
c、 M([]()->字符串{//编译器erorr C2668('function':对重载函数M的调用不明确)
返回(“txt”);
});
});
返回0;
}

您没有发布错误消息,因此通过查看我的水晶球,我只能断定您遇到了这些问题:

缺失#包括 你需要在顶端

#include <string>
#include <functional>

或 std::函数。。。 字符串

缺少函数
main()
与g合作++
foo@bar:$cat-lambda.cc
#包括
#包括
C类
{
公众:
void M(std::string t){}
void M(std::function func){}
};
void TestMethod(std::function func){}
void TestMethod2()
{
TestMethod([])(){
C C;
c、 M([]()->std::string{//编译器错误C2668
返回(“txt”);
});
});
}
int main(){
}
foo@bar:$g++-std=c++0x-lambda.cc

工作正常。

这是VC++编译器的一个bug


好的。那么C2668是怎么说的呢?不是每个人都在使用这个编译器。对不起。我在下面发布的版本上面添加了错误描述,该版本是用g++干净编译的。您应该发布您的实际代码。您的代码仍然不完整。要使用
字符串
,您应
#包括
。此外,标准兼容的
main
具有签名
int main()
int main(int,char*[])
。其他一切都是特定于编译器的。顺便说一句,到目前为止,您还尝试了什么?我将错误号(现在也发布了错误消息)作为源代码注释。我的问题是,编译器不知道应该使用M的哪个方法重载。但我不知道为什么。你说代码用g++编译是正确的,所以可能是VC++编译器的错误。或者是g++中的错误,也许不应该,但g++会;尽管就我个人而言,我不明白为什么VC++不能处理它。g++也有缺陷@Shaun Wilde:lambda的主体是一个
复合语句
,这与普通函数相同,其中主体也是一个
复合语句
(见附录a:语法摘要)。当然,每个非平凡的软件都有bug;)@phresnal-上面的代码(在问题正文中)并不像提问者所描述的那样在VC++中编译。它是否应该像在g++中那样,如果是这样,那么它就是vc++中的一个bug?@Shaun Wilde:这还取决于OP没有发布的其余代码。然而,我想这确实是VC++中的一个bug。
using namespace std;
using std::string; using std::function;
int main() {}
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