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()); }
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.
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.
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.
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.
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... }
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With