9

So I know C++ has a feature called "template template parameters", where you can pass a class template as a template parameter. For example:

template <typename T>
class vector { ... };

template <template <typename> class container>  // this is a template template parameter
class foo { ...  };

...

foo<vector> f;  // pass the vector template itself as template parameter

Is there an analogous thing for function templates? I.e. is there a way to pass a function template (for example, std::make_pair) as a template parameter to a class?

HighCommander4
  • 50,428
  • 24
  • 122
  • 194
  • What would be the magic syntax you would want to use? – MSN Aug 11 '11 at 21:57
  • @MSN: I was looking to write a function `template ?? F> void foo(F)` which can be called as `foo(bar)` where `bar` is a function template (e.g. `template void bar(T)`). `foo` could then call `F` with arguments of various types. I'll leave it up to your imagination as to what might go in the `???` :) This can be made to work if `bar` is a polymorphic function object (i.e. a non-template class with a templated operator()), in which case `foo`'s template parameter would be an ordinary type parameter. [continued in next comment] – HighCommander4 Aug 11 '11 at 22:02
  • [continued from previous comment] However, I would like to use an existing function template `bar` without adapting it to be a polymorphic function object. – HighCommander4 Aug 11 '11 at 22:05
  • so something like `template ?? F> void foo(F f) { f(1, 2); f(3.0); }`? You can't refer to a set of function overloads as a single type or template type, unfortunately. – MSN Aug 11 '11 at 22:22
  • @MSN: No, more like `template ?? F> void foo(F f) { f(Bar()); f(Moogah()); }` I don't need `???` to refer to a set of function overloads, just a set of instantiations of a single function template. (i.e. the thing I would pass as `f` would NOT be a function with an overload for `Bar` and an overload for `Moogah`; it would be a single function template, which `foo` would then instantiate with parameters of type `Bar` and `Moogah`. – HighCommander4 Aug 11 '11 at 22:24
  • @MSN: I asked this as a separate question here: http://stackoverflow.com/questions/7033645/is-there-a-generic-way-to-adapt-a-function-template-to-be-a-polymorphic-function – HighCommander4 Aug 11 '11 at 22:26
  • I came on SO to ask this exact question. Another C++ metaprogramming inconsistency :/ – Joseph Garvin Feb 15 '12 at 16:23

1 Answers1

12

No.

Mateen Ulhaq
  • 24,552
  • 19
  • 101
  • 135
Alexandre C.
  • 55,948
  • 11
  • 128
  • 197
  • @HighCommander4: you can use class templates and call static member functions. – Alexandre C. Aug 11 '11 at 21:41
  • @Alexandre C.: What I wanted to do was take some function templates that someone else wrote, and adapt them to be polymorphic function objects (without having to declare a separate class for each function template I'm adapting). I don't think that's possible without function template template parameters... – HighCommander4 Aug 11 '11 at 21:44
  • @HighCommander4: why don't you go with `boost::function` (or C++0x `std::function` / `std::tr1::function`) ? The type erasure they provide should get you out of trouble waters. – Alexandre C. Aug 11 '11 at 21:54
  • @Alexandre. C: And what would I put `std::function< --HERE-- >`? – HighCommander4 Aug 11 '11 at 22:06
  • @Alexandre. C: I'm taking this discussion to its own question: http://stackoverflow.com/questions/7033645/is-there-a-generic-way-to-adapt-a-function-template-to-be-a-polymorphic-function – HighCommander4 Aug 11 '11 at 22:27