Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to pass a template function in a template argument list

Suppose I have a template function:

template<typename T>
T produce_5_function() { return T(5); }

How can I pass this entire template to another template?

If produce_5_function was a functor, there would be no problem:

template<typename T>
struct produce_5_functor {
  T operator()() const { return T(5); }
};
template<template<typename T>class F>
struct client_template {
  int operator()() const { return F<int>()(); }
};
int five = client_template< produce_5_functor >()();

but I want to be able to do this with a raw function template:

template<??? F>
struct client_template {
  int operator()() const { return F<int>(); }
};
int five = client_template< produce_5_function >()();

I suspect the answer is "you cannot do this".

like image 465
Yakk - Adam Nevraumont Avatar asked Mar 27 '13 03:03

Yakk - Adam Nevraumont


People also ask

Can a template be a template parameter?

Templates can be template parameters. In this case, they are called template parameters. The container adaptors std::stack, std::queue, and std::priority_queue use per default a std::deque to hold their arguments, but you can use a different container.

How do you call a function in a template?

Defining a Function TemplateA function template starts with the keyword template followed by template parameter(s) inside <> which is followed by the function definition. In the above code, T is a template argument that accepts different data types ( int , float , etc.), and typename is a keyword.

Can we pass Nontype parameters to templates?

Template non-type arguments in C++It is also possible to use non-type arguments (basic/derived data types) i.e., in addition to the type argument T, it can also use other arguments such as strings, function names, constant expressions, and built-in data types.


2 Answers

I suspect the answer is "you cannot do this".

Yes, that is the case, you cannot pass a function template as a template argument. From 14.3.3:

A template-argument for a template template-parameter shall be the name of a class template or an alias template, expressed as id-expression.

The template function needs to be instantiated before you pass it to the other template. One possible solution is to pass a class type that holds a static produce_5_function like so:

template<typename T> struct Workaround {   static T produce_5_functor() { return T(5); } }; template<template<typename>class F> struct client_template {   int operator()() const { return F<int>::produce_5_functor(); } }; int five = client_template<Workaround>()(); 

Using alias templates, I could get a little closer:

template <typename T> T produce_5_functor() { return T(5); }  template <typename R> using prod_func = R();  template<template<typename>class F> struct client_template {   int operator()(F<int> f) const { return f(); } };  int five = client_template<prod_func>()(produce_5_functor); 
like image 97
Jesse Good Avatar answered Sep 23 '22 09:09

Jesse Good


How about wrapping that function?

template<typename T> struct produce_5_function_wrapper {     T operator()() const { return produce_5_function<T>(); } }; 

Then you can use the wrapper instead of the function:

int five = client_template< produce_5_function_wrapper >()(); 

Using the template function alone will not work, there's no such thing as "template template functions".

like image 39
mfontanini Avatar answered Sep 20 '22 09:09

mfontanini