Search code examples
c++templatesfunctor

Simpler syntax to invoke a template functor


I have a functor that looks like this:

struct Functor {
  template<typename T, typename... Args> static void func( Args&&... args );
};

I need to pass it as a template parameter to a function and this function invokes it:

template<typename F> void fn() {
  F::template func<int>(1, 2, 3);
}
fn<Functor>();

Having to write F::template func<int> feels tedious, and I need to call it many times in many different functions. I would love to be able to write something more concise, like F<int>(1, 2, 3).

Is there any way to sugarcoat it?


Solution

  • Another alternative is to let all template deducible, something like:

    template <typename> struct tag{};
    
    struct Functor {
      template<typename T, typename... Args> static void func(tag<T>,  Args&&... args );
    };
    

    And so, usage is similar to:

    template<typename F> void fn() {
      F::func(tag<int>{}, 1, 2, 3);
    }
    fn<Functor>();