I want to accept any lambda, so that it can perform some operation safely under the lock_guard and then return anything, but using as shown below throws an error:
#include <iostream>
#include <functional>
#include <mutex>
#include <memory>
class Test {
public:
template<typename Ret>
Ret DoLocked(std::function<Ret(Test&)> func) {
std::lock_guard<std::mutex> lock(*mtx);
return func(*this);
}
private:
std::unique_ptr<std::mutex> mtx = std::make_unique<std::mutex>();
};
int main() {
Test a;
a.DoLocked([](Test &s) {
std::cout << "in do locked" << std::endl;
});
return 0;
}
[build] /some-path/test.cc:21:6: error: no matching function for call to ‘Test::DoLocked(main()::<lambda(Test&)>)’
[build] 21 | });
[build] | ^
[build] /some-path/test.cc:9:13: note: candidate: ‘template<class Ret> Ret Test::DoLocked(std::function<Ret(Test&)>)’
[build] 9 | Ret DoLocked(std::function<Ret(Test&)> func) {
[build] | ^~~~~~~~
[build] /some-path/test.cc:9:13: note: template argument deduction/substitution failed:
[build] /some-path/test.cc:21:6: note: ‘main()::<lambda(Test&)>’ is not derived from ‘std::function<Ret(Test&)>’
[build] 21 | });```