codablank1
codablank1

Reputation: 6175

Is it possible to use typedef in a templated function signature?

Is there a way to use a typedef as an argument of a template function ? Or a way to define a type as an argument ?

let's say I want to pass a function pointer to this function :

template<typename C, typename ...Args>
void test(typedef void (C::*functor)(Args... args) functor f)
{
   f(args...);
}

Upvotes: 0

Views: 308

Answers (2)

R. Martinho Fernandes
R. Martinho Fernandes

Reputation: 234414

No you can't make a typedef in a parameter. If your goal is to avoid repeating the type of the parameter in the function body, you can use decltype:

template<typename C, typename ...Args>
void test(void (C::*f)(Args...))
{
   typedef decltype(f) functor;
}

Upvotes: 3

Sarfaraz Nawaz
Sarfaraz Nawaz

Reputation: 361352

No.

But why would you even want that when you can write this:

template<typename C, typename ...Args>
void test(void (C::*f)(Args...), Args... args)
{
   C c;   //f is a member function, so need an instance of class
   (c.*f)(args...);  //call the function using the instance.
}

Or, you can pass the instance along with arguments, or do something else. I assume it is just a proof-of-concept, and in the real code would be something else.

Upvotes: 0

Related Questions