如何传递方法并使用可变数量的参数调用它
How to pass method and call it with variable number of arguments
我有 class 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);
我有 class 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);