2
template<typename Type>
void execute(absl::string_view expected_name){
  std::cout << "*** expected type: " << expected_name
            << " | actual type: " << typeid(Type);
}

void handle(std::function<void(absl::string_view)> executor){
  executor<int>("int");
  executor<double>("double");
}

I have this piece of code (which, of course, didn't compile). I want to be able to pass a templated function to a "normal" function and have the "normal" function define the concrete type as it needs, as shown in the example.

Is there a way to declare on the handle parameter list that executor is a templated function?

max66
  • 65,235
  • 10
  • 71
  • 111
One Two Three
  • 22,327
  • 24
  • 73
  • 114
  • you cant pass templates as parameters to functions, you cant even have an instance of a template. You probably want `handle` to be a template too – 463035818_is_not_an_ai Feb 26 '18 at 17:12
  • Might be a duplicate of: https://stackoverflow.com/questions/5153148/how-to-use-stdfunction-to-point-to-a-function-template – JBL Feb 26 '18 at 17:12
  • If that was possible, I think you could come up with cases where you [edit: or the compiler] wouldn't be able to know which templates to instatiate before the execution. – Caninonos Feb 26 '18 at 17:14
  • @user463035818 `handle` being templated wouldn't solve the problem, would it? Even if I made `handle` templated, it still wouldn't change the fact that `executor` isn't declared to be templated – One Two Three Feb 26 '18 at 17:30
  • at some point you need to choose the parameters to instantiate `executor`. Either you do this directly at `handle` or the caller of `handle` decides – 463035818_is_not_an_ai Feb 26 '18 at 17:33
  • you should include the error message in the question, maybe I am just confused about what you actually want to do – 463035818_is_not_an_ai Feb 26 '18 at 17:34
  • Yes, I want handle() to choose the parameters of executor, NOT its caller. – One Two Three Feb 26 '18 at 17:34
  • If `handle` is a template function, it can accept a template parameter which is itself a template. "template template parameters" are fun. – Ben Voigt Feb 26 '18 at 17:49

1 Answers1

2

But handler() must receive necessarily a std::function?

If you can pass a struct with a template function in it, say

struct executor
 {
   template <typename T>
   void func (std::string_view const & sw) const
    { execute<T>(sw); }
 };

you can write handler() as follows

void handle (executor const & ex)
 { ex.func<int>("int"); ex.func<double>("double"); }
max66
  • 65,235
  • 10
  • 71
  • 111
  • The simple solutions are always the best. Very clean. +1 – super Feb 26 '18 at 17:48
  • Sure, this would work. But I didn't want to have to define an additional structure. Here you're wrapping a function inside a struct. C++ allows passing a function first class object, so I can't see why we can't make that work – One Two Three Feb 26 '18 at 17:49
  • 1
    @OneTwoThree: He's not wrapping one function in a struct, but an entire function template, which defines a family of functions. A function pointer may be a first class object, but the family of functions produced from a template, is not. – Ben Voigt Feb 26 '18 at 17:50
  • 2
    @OneTwoThree - unfortunately a template function isn't an object but a set of objects; you can select one of they and pass it as argument of a function, but you can't pass the whole set of functions. The `struct` trick avoid this problem and pass a single object with the whole set of functions inside it. – max66 Feb 26 '18 at 17:51
  • And even for the "template template parameter" approach I mentioned in a comment, the template has to be a template class, not a template function. See also https://stackoverflow.com/q/15651488/103167 – Ben Voigt Feb 26 '18 at 17:55