如何在C ++ 11中将lambda表达式存储为类的字段? [英] How can I store a lambda expression as a field of a class in C++11?

查看:130
本文介绍了如何在C ++ 11中将lambda表达式存储为类的字段?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想创建一个类,客户端可以存储一个lambda表达式 []() - > void {} 作为类的一个字段,但我不知道如何做。 一个回答建议使用 decltype ,我尝试没有成功。以下是 ideone来源链接。以下是源和结果:

I'd like to create a class where the client can store a lambda expression like []() -> void {} as a field of the class, but I can't figure out how to do so. One answer suggested using decltype, which I tried with no success. Here is a ideone source link. The below is the source and result:

#include <cstdio>
auto voidLambda = []()->void{};

class MyClass {
public:
     decltype(voidLambda) t;
     MyClass(decltype(voidLambda) t) { 
        this->t = t;
     }
};

int main() {
   MyClass([] {
      printf("hi");
   });
}

结果:

prog.cpp: In constructor 'MyClass::MyClass(<lambda()>)':
prog.cpp:3:79: error: no matching function for call to '<lambda()>::__lambda0()'
prog.cpp:2:20: note: candidates are: <lambda()>::<lambda>(const<lambda()>&)
prog.cpp:2:20: note:                 <lambda()>::<lambda>(<lambda()>&&)
prog.cpp:3:88: error: no match for 'operator=' in '((MyClass*)this)->MyClass::t = t'
prog.cpp: In function 'int main()':
prog.cpp:5:27: error: no matching function for call to 'MyClass::MyClass(main()::<lambda()>)'
prog.cpp:3:48: note: candidates are: MyClass::MyClass(<lambda()>)
prog.cpp:3:14: note:                 MyClass::MyClass(const MyClass&)

有没有人知道如何做?

推荐答案

如果你想要一个类成员是一个lambda表达式,考虑使用 std :: function<> 任何可调用函数。例如:

If you want a class member to be a lambda expression, consider using the std::function<> wrapper type, which can hold any callable function. For example:

std::function<int()> myFunction = []() { return 0; }
myFunction(); // Returns 0;

这样,你不需要知道lambda表达式的类型。您只需存储相应函数类型的 std :: function<> ,模板系统将为您处理所有类型。更一般地,即使该函子的实际类型是匿名的,也可以将适当签名的任何可调用实体分配给 std :: function<>

This way, you don't need to know the type of the lambda expression. You can just store a std::function<> of the appropriate function type, and the template system will handle all the types for you. More generally, any callable entity of the appropriate signature can be assigned to a std::function<>, even if the the actual type of that functor is anonymous (in the case of lambdas) or really complicated.

std :: function 模板中的类型应该是函数类型对应于您要存储的函数。因此,例如,要存储一个接受两个 int s并返回void的函数,您将创建一个 std :: function< void int,int)> 。对于没有参数并返回 int 的函数,您可以使用 std :: function< int()> 。在你的情况下,因为你想要一个没有参数并返回 void 的函数,你会想要这样:

The type inside of the std::function template should be the function type corresponding to the function you'd like to store. So, for example, to store a function that takes in two ints and returns void, you'd make a std::function<void (int, int)>. For a function that takes no parameters and returns an int, you'd use std::function<int()>. In your case, since you want a function that takes no parameters and returns void, you'd want something like this:

class MyClass { 
public:
    std::function<void()> function;
    MyClass(std::function<void()> f) : function(f) {
        // Handled in initializer list
    }
};

int main() {
    MyClass([]() -> void {
        printf("hi")
    }) mc; // Should be just fine.
}

希望这有助!

这篇关于如何在C ++ 11中将lambda表达式存储为类的字段?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆