Reputation: 917
I have C++ class which is template. it have member function which should take any lamda as parameter;
basically this is what I wanna do:-
#include <QFuture>
#include <QFutureWatcher>
template <class T>
class EFuture {
private:
QFuture<T> future;
QFutureWatcher<T> watcher;
public:
explicit EFuture(QFuture<T> &future);
void onFinished(void (*f)() );
};
template <class T>
EFuture<T>::EFuture(QFuture<T> &future ): future(future)
{ }
template<class T>
void EFuture<T>::onFinished(void (*f)()){
QObject::connect(watcher,&QFutureWatcher<T>::finished,f);
watcher.setFuture(future);
}
This have serious restriction as I can't capture anything in lambda which I am passing. where I try to do something like this:-
future->onFinished([someobject](){
...
});
I get following error:-
connectionworker.cpp:106:24: error: no viable conversion from '(lambda at /home/noone/Development/Exatation/Exever/src/connectionworker.cpp:106:24)' to 'void (*)()'
efuture.h:17:28: note: passing argument to parameter 'f' here
Upvotes: 0
Views: 730
Reputation: 48447
Only non-capturing and non-generic lambda expressions are convertible to a function pointer. Itself, any lambda expression -- both captureless and capturing ones -- has its own type, known only to the compiler. In such a case, there are two alternatives:
Use a function template that can deduce the type of the lambda expression:
template <typename F>
void onFinished(F f);
Use a type-erasing technique, e.g., std::function<void()>
:
#include <functional>
void onFinished(std::function<void()> f);
Upvotes: 2