0

I have a function defined like this:

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);
}

and I would like to pass it into a function like this:

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...);
   }
};

I would like to pass the getter function to the parameter fn of the function index_filter::execute above. I want to know if there is a way to achieve this. So I can pass any functions like getter to implement different functions by using the same struct index_fiter.
Using it like this:

 poker_index out;
 index_filter<4>::execute(getter, rep, out);
Keanyuan
  • 206
  • 1
  • 9

1 Answers1

1

I don't know how you would be able to do it exactly like this, but given that getter looks like a free function (although apparently a static one), you could move it into a struct:

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
};

You can then pass the SpecificGetImplementation type as a template argument to your execute and have that function invoke the getter member:

template <typename GI>
void execute(GI const&) {
  GI::getter<Arguments>(arguments);
}

execute(SpecificGetImplementation{});

Then you can also implement alternatives to SpecificGetImplementation in separate structs.

bitmask
  • 32,434
  • 14
  • 99
  • 159
  • Besides, should it be `typename... Arguments`? – user202729 Feb 01 '21 at 06:42
  • @user202729 Yes, I saw the dupe after writing my answer. And `Arguments` is a place holder here for whatever stuff you want inside the template argument list. – bitmask Feb 01 '21 at 06:43