C ++ 11 Lambda表达式作为回调函数 [英] C++11 Lambda Expressions as Callback Functions

查看:1168
本文介绍了C ++ 11 Lambda表达式作为回调函数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

有没有任何C ++ GUI工具包支持将回调函数定义为C ++ 11 lambda表达式?我相信这是一个独特的使用C#(至少与C ++相比)编写基于GUI的程序。对于使用lambda表达式作为参数的函数,我应该使用什么类型的签名?如何支持隐式转换?

Does any C++ GUI toolkit out there support definition of callback functions as C++11 lambda expressions? I believe this is a unique pro of using C# (compared to C++ at least) for writing GUI-based programs. What type signature should I use for functions taking lambda expressions as arguments and how does these support implicit conversions?

推荐答案


是否有任何C ++ GUI工具包支持将回调函数定义为C ++ 11 lambda表达式?

Does any C++ GUI toolkit out there support definition of callback functions as C++11 lambda expressions?

指针,那么你至少可以使用不捕获任何东西的lambdas。这样的lambdas可以自动转换为函数指针。

If they accept function pointers then you can at least use lambdas that don't capture anything. Such lambdas can be automatically converted to function pointers.


我们应该使用什么类型签名作为lambda表达式作为参数,隐式转换?

What type signature should I use for functions taking lambda expressions as arguments and how does these support implicit conversions?

如果你想让人们使用lambdas或任何可调用对象,你可以让你的API接受std :: function对象,或使用模板:

If you want people to use lambdas or any callable object then you could either have your API accept std::function objects, or use a template:

template<typename Callback>
void do_it(Callback c) {
    c();
}

do_it([&]{ c = a+b; });

模板将允许lambda被内联,而std :: function需要间接。这对于GUI回调可能无关紧要。

A template will allow the lambda to be inlined while std::function require indirection. This may not matter much for GUI callbacks.

这篇关于C ++ 11 Lambda表达式作为回调函数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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