没有类型名的模板参数

Template argument without typename

我正在阅读关于 SO 的 ,我想知道 struct C(没有 typename 关键字的那个)的第二个模板参数的含义是什么。

代码如下:

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)...);
    }
};

我知道代码在做什么,但我不明白 template<typename T, T> struct C; 声明的第二个 T 的目的及其在没有 typename 关键字的情况下的含义。

有人能告诉我它的意思吗?感谢您的回答。

T 已经定义在第一个 typename T,

因此,如果您在第二个 T 中添加 class 或再次添加 typename T,您将得到:

error C2991: redefinition of template parameter 'T'

是模板值参数。

template<typename T, T> struct C;

意思是你定义了类型T,然后也传了一个T类型的值给模板。在 SO 问题的示例中,类型是函数指针类型,然后第二个 T 的值是指向匹配类型函数的实际指针。