David
David

Reputation: 674

How to use typename instead of typedef?

I have the following code snippet:

template <class T>
int foo(T (*f)()) {
    typedef T (*func)();
    typedef functor<T, func> F; //this line 

    // ...
}

As you can see, I use typedef for a function pointer (func), but I want to remove it to simplify my code. I tried this:

template <class T>
int foo(T (*f)()) {
    typedef functor<T, typename f> F;

    // ...
}

But that doesn't compile. What is the right way to to spell that full typedef for F in a single line?

Upvotes: 1

Views: 161

Answers (1)

Barry
Barry

Reputation: 304142

Just put in the actual type of f:

typedef functor<T, T(*)()> F;

Or you could use decltype:

typedef functor<T, decltype(f)> F;

Or you could write an alias for such a thing:

template <typename T>
using ReturnT = T(*)();

template <typename T>
int foo(ReturnT<T> f) {
    using F = functor<T, ReturnT<T>>;
    // ...
}

Upvotes: 2

Related Questions