简体   繁体   English

lambda表达式被拒绝(C ++ 11)

[英]lambda expression being rejected (C++11)

virtual Answer<const Taaal& const> askWho(bool(const Taaal& const)) = 0;
virtual Answer<const Taaal& const> askWho(const Taaal& const t) {
    return askWho([&](auto q) {
        return q == t;
    });
}

is being rejected with "no instance of overloaded function matches the argument list" (additionally, I'm being told auto is not allowed, but replacing that with an explicit type leaves the first problem) 被拒绝为“没有重载函数的实例与参数列表匹配”(此外,我被告知不允许使用auto,但是用显式类型替换它会导致第一个问题)

Taaal is the (abstract) class that these methods are declared in. Answer is #included from another header, and its definition shouldn't matter. Taaal是在其中声明这些方法的(抽象)类。答案是从另一个标头中包含的,并且它的定义无关紧要。

Only a capture-less lambda defines an implicit conversion to a function pointer. 仅无捕获的lambda定义对函数指针的隐式转换。 Your lambda expression captures its context because of the capture default & . 由于捕获默认为& ,因此您的lambda表达式将捕获其上下文。 Since your lambda cannot be stateless, you could change the parameter type of the other overload 由于您的lambda不能是无状态的,因此您可以更改另一个重载的参数类型

virtual Answer<const Taaal&> askWho(std::function<bool(const Taaal&)>) = 0;

If your lambda didn't need to capture context variables, you could've dropped the & from the capture list and the conversion would've worked as expected 如果您的lambda不需要捕获上下文变量,则可以从捕获列表中删除& ,转换将按预期进行

virtual Answer<const Taaal&> askWho(const Taaal& t) {
    return askWho([](auto q) {
//                ^^ - nothing here
        return ...;
    });
}

Note that the auto q parameter type for the lambda only works if you compile your code in C++14 mode ( -std=c++1y on gcc and clang). 请注意,仅当您在C ++ 14模式下编译代码时才使用lambda的auto q参数类型(在gcc和clang上为-std=c++1y )。 In C++11 mode you'll have specify the parameter type. 在C ++ 11模式下,您将指定参数类型。

auto in lambdas is only allowed in C++14. Lambdas中的auto仅在C ++ 14中允许。

You must use the real parameter type, polymorphic lambdas are not included in C++11. 您必须使用实数参数类型,C ++ 11中不包含多态lambda。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM