假设我有一个包含lambda的变量:
Say I have the following variable containing a lambda:
auto a = [] { return true; };我想要 a c $ c> false 。我可以按照这样做吗?
And I want a to return false later on. Could I do something along the lines of this?
a = [] { return false; };此语法给我以下错误:
binary '=' : no operator found which takes a right-hand operand of type 'main::<lambda_a7185966f92d197a64e4878ceff8af4a>' (or there is no acceptable conversion) IntelliSense: no operator "=" matches these operands operand types are: lambda []bool ()->bool = lambda []bool ()->bool有没有办法实现这样的东西?我想将 auto 变量更改为不同的lambda。我相当一个初学者,所以我可能会缺少一些关于 auto 或lambdas的知识。谢谢。
Is there any way to achieve something like this? I would like to change the auto variable to a different lambda. I'm relitively a beginner so I may be missing some knowledge about auto or lambdas. Thanks.
推荐答案每个lambda表达式都会创建一个新的唯一类型,因此您的第一个lambda类型不同于您的秒(示例)。此外,lambda的副本赋值运算符定义为已删除(示例),因此您无法做这个。为了类似的效果,你可以有 a 是一个 std :: function 对象,
Every lambda expression creates a new unique type, so the type of your first lambda is different from the type of your second (example). Additionally the copy-assignment operator of a lambda is defined as deleted (example) so you're doubly-unable to do this. For a similar effect, you can have a be a std::function object though it'll cost you some performance
std::function<bool()> a = [] { return true; }; a = [] { return false; };更多推荐
C ++ 11将`auto` Lambda更改为不同的Lambda?
发布评论