Ale*_*der 1 c++ templates variadic-templates
基于我之前的问题,这里有Variadic模板,函数作为参数
我最终得到了一个简单的类,但是如何使用std :: invoke或其他方法调用Tester :: test作为参数?http://en.cppreference.com/w/cpp/utility/functional/invoke#Example提供的示例对我没有多大帮助,我尝试了许多使用std :: invoke的不同方法.
class Tester {
public:
template<typename F, typename... Args>
decltype(auto) test(F&& f, Args&&... args) {
return std::forward<F>(f)(std::forward<Args>(args)...);
}
int simple(int i) { return i; }
};
int main()
{
Tester t;
std::cout << t.test(t.simple, 3); // how do you modify this line such that the code compiles?
}
Run Code Online (Sandbox Code Playgroud)
谢谢!
一种方法是修改test()
成员函数的定义,如下所示:
class Tester {
public:
template<typename F, typename... Args>
decltype(auto) test(F&& f, Args&&... args) {
return std::invoke(std::forward<F>(f), std::forward<Args>(args)...);
}
int simple(int i) { return i; }
};
Run Code Online (Sandbox Code Playgroud)
然后我们可以将一个成员函数指针传递给test()
,应该将该方法作为第二个参数调用的实例,然后是函数参数本身:
int main()
{
Tester t;
std::cout << t.test(&Tester::simple, t, 3);
}
Run Code Online (Sandbox Code Playgroud)
这部作品的原因是因为std::invoke
有一个为指针到成员特殊过载,从而
std::invoke(ptr-to-member, instance, args...);
Run Code Online (Sandbox Code Playgroud)
被称为
(instance.*ptr-to-member)(args...);
Run Code Online (Sandbox Code Playgroud)
这正是我们在这种情况下想要的.
归档时间: |
|
查看次数: |
636 次 |
最近记录: |