我正在阅读the following question and its associated accepted answer 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)...);
}
};
我知道代码在做什么,但我不知道第二个 T
的目的template<typename T, T> struct C;
的没有 typename
的声明及其含义关键字。
谁能告诉我它的含义吗?感谢您的回答。
最佳答案
这是模板值参数。
template<typename T, T> struct C;
表示您定义类型 T,然后还将类型 T 的值传递到模板中。在 SO 问题的示例中,类型是函数指针类型,然后第二个 T 的值是指向匹配类型的函数的实际指针。
关于c++ - 没有类型名的模板参数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43030939/