This is a rather unusual problem – but a problem nonetheless. I have a function that accepts a lambda as a parameter and then passes it on to QObject::connect:
template<typename Functor>
void MyClass::doSomething(Functor f)
{
connect(network_reply, &QNetworkReply::finished, f);
//...
}
A sample invocation of MyClass::doSomething might look like this:
doSomething([]()
{
// how do I get access to the sender???
});
As you can tell by my comment, there is no way to access QObject::sender to get a pointer to the QObject that emitted the signal. Because the class emitting the signal is not in the scope that the lambda is being created in, there is no way of referencing it inside the lambda.
What options do I have?
Edit: I tried using Andy’s suggestion (std::bind) but I end up getting one of those template errors that are nearly impossible to understand:
http://paste.ubuntu.com/1614425/
Excerpt:
decltype cannot resolve address of overloaded function
…and it points to the QObject::connect call.
If I understood your requirements correctly, you can first let your lambda functor accept an argument of the appropriate type (passing by reference here, so take care of lifetime issues):
Then, your
doSomething()template function could usestd::bind()to make sure your lambda will be invoked with the appropriate object as its argument: