我有一个这样定义的函数:
template<int... rests>
static std::enable_if_t<sizeof...(rests) == 0, void> getter(pokers_rep& rep, poker_index& out) {
}
template<int index, int... rests>
static void getter(pokers_rep& rep, poker_index& out) {
out |= rep.indices<index>();
getter<rests...>(rep, out);
}
我想把它传递给这样的函数:
template<int N, int... rests>
struct index_filter :public index_filter<N - 1, N - 1, rests...> {
};
template<int... rests>
struct index_filter<1, rests...>
{
template< template<int... fntps> class Fn, class... Args>
inline static void execute(Fn<rests...>&& fn, Args&&... args) {
fn(args...);
}
};
我想把getter函数传给上面函数index_filter::execute的参数fn。
我想知道是否有办法实现这一目标。所以我可以通过使用相同的struct index_fiter来传递任何像getter这样的函数来实现不同的功能。
像这样使用它:
poker_index out;
index_filter<4>::execute(getter, rep, out);
答案 0 :(得分:1)
我不知道你怎么能完全像这样做到这一点,但考虑到 getter
看起来像一个自由函数(虽然显然是一个静态函数),你可以将它移动到一个结构中:< /p>
struct SpecificGetImplementation {
template <typename Arguments>
static void getter(Arguments);
// beware that static means something different here.
// if you want to mimic the original effect wrap the entire
// struct into an anonymous namespace
};
然后您可以将 SpecificGetImplementation
类型作为模板参数传递给您的 execute
并让该函数调用 getter
成员:
template <typename GI>
void execute(GI const&) {
GI::getter<Arguments>(arguments);
}
execute(SpecificGetImplementation{});
然后,您还可以在单独的结构中实现 SpecificGetImplementation
的替代方案。