Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

C++ method name as template parameter

Tags:

c++

templates

How do I make the method name (here some_method) a template parameter?

template<typename T> void sv_set_helper(T& d, bpn::array const& v) {   to_sv(v, d.some_method()); } 
like image 380
Neil G Avatar asked Sep 26 '11 16:09

Neil G


People also ask

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 a template parameter be a function?

A template parameter is a special kind of parameter that can be used to pass a type as argument: just like regular function parameters can be used to pass values to a function, template parameters allow to pass also types to a function.

Which is correct example of template parameters?

For example, given a specialization Stack<int>, “int” is a template argument. Instantiation: This is when the compiler generates a regular class, method, or function by substituting each of the template's parameters with a concrete type.

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.


1 Answers

Here is a simple example...

#include <iostream>  template<typename T, typename FType> void bar(T& d, FType f) {   (d.*f)(); // call member function }   struct foible {   void say()   {     std::cout << "foible::say" << std::endl;   } };  int main(void) {   foible f;   bar(f,  &foible::say); // types will be deduced automagically... } 
like image 173
Nim Avatar answered Sep 28 '22 06:09

Nim