jeudi 27 avril 2017

Variadic signals and generic lambdas

Is it possible to create variadic signal and connect generic lambda as slot? I mean something like (say, all definitions of involved functions are visible where needed (e.g. at points of instantiation)):

#include <QCoreApplication>
#include <QObject>
#include <QTime>

class A
    : public QObject
{
    Q_OBJECT

public :

    A(QObject * const parent = Q_NULLPTR)
        : QObject{parent}
    { ; }

signals :

    template< typename ...Ts >
    void infoMessage(Ts... args);

public slots :

    void run()
    {
        emit infoMessage("Started at ", QTime::currentTime());
    }

};

#include <QTimer>
#include <QtDebug>

#include "main.moc"

int main(int argc, char * argv [])
{
    QCoreApplication a{argc, argv};
    A a;
    auto printInfoMessage = [&] (auto... args)
    {
        (qInfo() << ... << args);
    };
    QObject::connect(&a, SIGNAL(infoMessage), printInfoMessage);
    QTimer::singleShot(0, &a, &A::run);
    return a.exec();
}

Currently it gives an error message:

AUTOGEN: error: process for main.cpp:18: Error: Template function as signal or slot

moc failed...

Here macro SLOT() instead of &A::infoMessage does not help a lot. Is there any workarounds to overcome this limitation?

I know, that some of the answers will contain a using of std::make_tuple and std::index_sequence stuff. But is there less verbose solution?

Aucun commentaire:

Enregistrer un commentaire