C++ 如何在自己的方法中使用lambda表达式
我喜欢新的语法 我想实现我自己的方法,该方法将lambda表达式作为参数 在我的特殊情况下,我想重新创建Javascript函数 将lambda作为我的方法的参数的正确语法是什么样子的?我希望lambda不要有任何参数或void以外的其他返回类型C++ 如何在自己的方法中使用lambda表达式,c++,qt,lambda,C++,Qt,Lambda,我喜欢新的语法 我想实现我自己的方法,该方法将lambda表达式作为参数 在我的特殊情况下,我想重新创建Javascript函数 将lambda作为我的方法的参数的正确语法是什么样子的?我希望lambda不要有任何参数或void以外的其他返回类型 void setTimeout(/* SOME_TYPE callback */, int timeout) { QTimer *timer = new QTimer(this); timer->setInterval(timeo
void setTimeout(/* SOME_TYPE callback */, int timeout)
{
QTimer *timer = new QTimer(this);
timer->setInterval(timeout);
timer->setSingleShot(true);
connect(timer, &QTimer::timeout, (){
//This lambda should be an argument
});
//connect(timer, &QTimer::timeout, callback);
connect(timer, &QTimer::timeout, timer, &QObject::deleteLater);
timer->start();
}
我找不到QObject中定义的方法。有些仅为生成文档而定义。(
#ifdef Q_QDOC
)您可以使用模板
并使用常量&
,这也具有快速处理函子的优点-或者您可以使用标准::函数
,这稍微慢一点,但也可以由虚拟函数执行。从标题functional`中创建一个参数,该参数应该接受适当模板类型的lambdastd::function'。您不必重新实现它:QTimer::singleShot
完成所有操作
如果您想包装单发
或连接
,您并不真正关心可调用的内容,只要目标方法接受它。onTimeout
没有理由成为类成员。它应该是一个免费的函数:我们正在编写C++,而不是java。
// https://github.com/KubaO/stackoverflown/tree/master/questions/forward-callable-38126723
#include <QtCore>
template <typename C>
void onTimeout(int msec, C && callable) {
QTimer::singleShot(msec, std::forward<C>(callable));
}
template <typename C>
void onTimeout(int msec, QObject * context, C && callable) {
QTimer::singleShot(msec, context, std::forward<C>(callable));
}
int main(int argc, char ** argv) {
QCoreApplication app{argc, argv};
QObject context;
QThread thread;
context.moveToThread(&thread);
thread.start();
onTimeout(1000, []{ qDebug() << "T+1s"; });
onTimeout(2000, &context, [&]{ qDebug() << "T+2s"; thread.quit(); });
QObject::connect(&thread, &QThread::finished, &app, &QCoreApplication::quit);
return app.exec();
}
//https://github.com/KubaO/stackoverflown/tree/master/questions/forward-callable-38126723
#包括
模板
void onTimeout(整数毫秒,C&&可调用){
QTimer::singleShot(毫秒,std::forward(可调用));
}
模板
void onTimeout(整数毫秒,QObject*上下文,C&&可调用){
QTimer::singleShot(毫秒,上下文,std::forward(可调用));
}
int main(int argc,字符**argv){
QCoreApplication{argc,argv};
对象语境;
QThread线程;
moveToThread(&thread);
thread.start();
onTimeout(1000,[]{qDebug()template void setTimeout(Callback Callback,int timeout){connect(…,Callback);}
是否允许已捕获的lambda?@IgorTandetnik:只要不必在虚拟函数中使用它,这确实是最好的。
// https://github.com/KubaO/stackoverflown/tree/master/questions/forward-callable-38126723
#include <QtCore>
template <typename C>
void onTimeout(int msec, C && callable) {
QTimer::singleShot(msec, std::forward<C>(callable));
}
template <typename C>
void onTimeout(int msec, QObject * context, C && callable) {
QTimer::singleShot(msec, context, std::forward<C>(callable));
}
int main(int argc, char ** argv) {
QCoreApplication app{argc, argv};
QObject context;
QThread thread;
context.moveToThread(&thread);
thread.start();
onTimeout(1000, []{ qDebug() << "T+1s"; });
onTimeout(2000, &context, [&]{ qDebug() << "T+2s"; thread.quit(); });
QObject::connect(&thread, &QThread::finished, &app, &QCoreApplication::quit);
return app.exec();
}