Home > other >  Pass function template as argument to normal function to be specialised differently within the norma
Pass function template as argument to normal function to be specialised differently within the norma

Time:01-13

I would like to do something to the following effect:

template <class A>
A doThings(A input) { /* do something */ }

void doTwoThings(ClassB inputB, ClassC inputC, template <class A> A(A) func) // This syntax is wrong but how to achieve this effect?
{
  std::cout << func<ClassB>(inputB);
  std::cout << func<ClassC>(inputC);
}

doTwoThings(5, "testStr", doThings);

Basically, I am trying to pass a function template as an argument to a normal function, and within that function, specialise the function template into two or more overloads and use them. Is there a way to do that in C ?

CodePudding user response:

Not sure if it match your need, but you might do

template <typename F>
void doTwoThings(ClassB inputB, ClassC inputC, F func)
{
  std::cout << func(inputB);
  std::cout << func(inputC);
}

doTwoThings(5, "testStr", [](const auto& input){ doThings(input); });
  •  Tags:  
  • Related