Pass a template template argument without specifying the concrete type

  • A+
Category:Languages

I would like to implement a wrapper around a function call that would do something like this:

template <template<class> class F> void Wrapper(int n, F&& f) {     switch (n)     {         case 1:             f<std::int8_t>();             break;         case 2:             f<std::int16_t>();             break;         default:             break;     } }  template <class T> void func() {     // ... body of func() } 

So that I would be able to make the following call in the code:

Wrapper(1, func); 

But the abovementioned code doesn't compile because F&& f construct is invalid - I need to specify the concrete type of the argument. But if I make the function signature the following:

template <template<class> class F, class T> void Wrapper(int n, F<T>&& f) 

then I must make the call with the concrete type of f:

Wrapper(1, func<std::int8_t>); 

and I won't be able to make a switch in Wrapper.

How can I implement the behaviour I need?

 


If you know func at compile time (that is, if it is not some function pointer), you can use the following solution:

template <template <class> typename F> void Wrapper(int n) {   switch (n) {     case 1: F<std::int8_t>{}(); break;     case 2: F<std::int16_t>{}(); break;     default: break;   } }  template <typename T> void func() { std::cout << sizeof(T) << std::endl; }  template <typename T> struct Func { void operator()() { func<T>(); } };  int main() {   Wrapper<Func>(1);   Wrapper<Func>(2); } 

Comment

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen: