任何人都知道是否可以做类似以下的事情:
void my_inner_func(int i, double d, ...) {
// do stuff here
}
template <typename Func, typename... Args>
void my_outer_func(Func&& f,Args&&... args){
// do other stuff here, put this inside loop, etc
f(std::forward<Args>(args)...);
}
template <typename OuterFunc,typename InnerFunc,typename... Args>
void func(OuterFunc&& outer, InnerFunc&& inner, Args&&...args) {
outer(std::forward<InnerFunc>(inner),std::forward<Args>(args)...);
}
调用代码可能如下所示:
func(my_outer_func, my_inner_func, 1, 2.0, ...);
我遇到的问题是无法推断出 OuterFunction 的类型。我尝试了很多不同的事情,包括将 func 转换为仿函数并使 OuterFunc 成为模板模板参数。
如果您创建my_outer_func
一个函数对象而不是函数,它会起作用:
struct my_outer_func {
template <typename Func, typename... Args>
void operator()(Func&& f,Args&&... args) const {
// do other stuff here, put this inside loop, etc
f(std::forward<Args>(args)...);
}
};
然后:
func(my_outer_func(), my_inner_func, 1, 2.0);
如果您不喜欢调用站点的括号,则可以在创建全局结构实例时删除它们:
struct {
template <typename Func, typename... Args>
void operator()(Func&& f,Args&&... args) const {
// do other stuff here, put this inside loop, etc
f(std::forward<Args>(args)...);
}
} my_outer_func;
本文收集自互联网,转载请注明来源。
如有侵权,请联系 [email protected] 删除。
我来说两句