Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/cplusplus/149.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
C++ 试图在静态函数(C+;+;)中使用lambda函数_C++ - Fatal编程技术网

C++ 试图在静态函数(C+;+;)中使用lambda函数

C++ 试图在静态函数(C+;+;)中使用lambda函数,c++,C++,为什么这个代码无效?。我不知道我是否正确使用了lambda sintax,但根据其他帖子,它看起来不错 struct foo{ static int faa(int x); }; int foo::faa(int x){ bool isCon = []() { return true; }; return isCon(); } lambda具有不确定的类型,因此您无法知道将使用哪种类型。显然,您定义的lambda不是bool类型(它可能返回bo

为什么这个代码无效?。我不知道我是否正确使用了lambda sintax,但根据其他帖子,它看起来不错

struct foo{
    static int faa(int x);
};

int foo::faa(int x){
    bool isCon = []() {
       return true;
    };

    return isCon();
}

lambda具有不确定的类型,因此您无法知道将使用哪种类型。显然,您定义的lambda不是bool类型(它可能返回bool,但不是bool),因此您可以这样做:

struct foo{
    static int faa(int x);
};

int foo::faa(int x){
    auto isCon = []()->bool {
       return true;
    };

    return isCon();
}
这里,
auto
关键字告诉编译器为您推断类型。
->bool
表达式告诉编译器lambda将返回bool


但是,您的
foo::faa()
函数返回一个int,因此cast可能会出现ocurr,因为您的lambda可能会返回bool(这与问题无关,但请注意)。

您得到的具体错误是什么?lambda会产生一个不确定的类型,而不是
bool
,请使用
自动isCon=
相关: