问题描述
我有类 CallProtector
应该使用可变数量的参数调用方法,这些参数假设通过互斥锁保护调用,但我不知道如何使用它们的参数传递对象的方法。这是我目前所拥有的:
class CallProtector
{
public:
template<typename F,typename ...Args>
void callProtectedMethod(F& lambda,Args... args)
{
std::lock_guard<std::mutex> guard(m_guard);
lambda(args);
}
private:
std::mutex m_guard;
};
我正在尝试以这种方式使用它:
class Car
{
public:
void updateEngine(int engineModelType) {}
};
int main()
{
Car alfaRomeo;
CallProtector callProtector;
callProtector.callProtectedMethod(&Car::updateEngine,10);
return 0;
}
no instance of function template "CallProtector::callProtectedMethod" matches the argument list
感谢任何帮助,提前致谢。
解决方法
从 C++17 开始,您可以使用 std::invoke
,只需将所有参数转发给它:
template<typename ...Args>
void callProtectedMethod(Args&&... args)
{
std::lock_guard<std::mutex> guard(m_guard);
std::invoke(std::forward<Args>(args)...);
}
同样,如果你想在 Car 实例上调用成员函数,你必须将指针传递给对象。
,以下内容可能对您有用:
class CallProtector
{
public:
template<typename F,typename ...Args>
void callProtectedMethod(F&& func,Args&&... args)
{
std::lock_guard<std::mutex> guard(m_guard);
func(std::forward<Args>(args)...);
}
private:
std::mutex m_guard;
};
然后像这样使用它:
Car alfaRomeo;
CallProtector callProtector;
auto updateEngine = std::bind(&Car::updateEngine,&alfaRomeo,std::placeholders::_1);
callProtector.callProtectedMethod(updateEngine,10);
编辑
或者这也行:
template<typename F,typename ...Args>
void callProtectedMethod(F&& func,Args&&... args)
{
std::lock_guard<std::mutex> guard(m_guard);
std::invoke(std::forward<F>(func),std::forward<Args>(args)...);
}
然后
callProtector.callProtectedMethod(&Car::updateEngine,alfaRomeo,10);