Search code examples
c++templatestypename

Template argument without typename


I was reading the the following question and its associated accepted answer on SO and I was wondering what was the meaning of the second template parameter of the struct C (the one without the typename keyword).

Here is the code:

template<typename T, T> struct C; // Here.

template<typename T, typename R, typename ...Args, R (T::*F)(Args...)>
struct C<R (T::*)(Args...), F> 
{
    R operator()(T &obj, Args&&... args) 
    {
        return (obj.*F)(std::forward<Args>(args)...);
    }
};

I know what the code is doing but I do not figured out the purpose of the second T of the template<typename T, T> struct C; declaration and its meaning without the typename keyword.

Could someone tell me its meaning? Thanks for your answers.


Solution

  • It's template value parameter.

    template<typename T, T> struct C;
    

    Means that you define the type T, then also pass a value of type T into the template. In the example in the SO question, the type was a function pointer type, then the value for the second T was an actual pointer to a function of matching type.