0

Is it possible to pass a function that takes a function pointer with a template argument? So for example I would like to have a function that may look something like this:

template<template<int I> typename Func>
void funcCaller(Func<I> func)
{
   func<1>();
   func<2>();
   func<10>();
}

and ideally could be used like this

template<int I>
void test()
{
   std::cout << I << std::endl;
}
funcCaller(test);

Now this doesn't compile, and a realize I am not using the templates in funcCaller correctly. Honestly I'm not even sure if what I am going for is possible, and it sure seems like a streach for the compiler to figure this out. I also don't want to specify the function signature, and the use case I have in mind would use this with a variadic-template, where funcCall acts as a loop of sorts that could operate a variety of functions.

Any thoughts on how to approach something like this? Any direction is much appreciated :)

If this is answered elsewhere I would love to know, and figure out what terminology I am missing.

  • If you want to pass an actual pointer as parameter, you have to pass a concrete function. `test` as is is just a template. Until it is instantiated with a template parameter (`test<3>` for example), there is nothing to point to. You could however probably pass the function as a template parameter. The end result would be something like `funcCaller()`. – ElderBug Dec 22 '22 at 03:04
  • See [Why can't template functions be passed as a template template parameter?](https://stackoverflow.com/questions/56904980/why-cant-template-functions-be-passed-as-a-template-template-parameter) – Jason Dec 22 '22 at 03:15

0 Answers0