为什么我不能这样做:
template<template<class E>class Derived>
struct X
{
static void f()
{
Derived<E>::value;
}
};
我遇到的问题是我无法编译此代码,因为我收到一条错误消息,指出参数 E 尚未声明。有没有办法让我可以使用或不使用这个形式参数?
最佳答案
模板模板参数的参数不获取参数,因此通常没有名称。部分特化是这条规则的异常(exception)。试试这个:
template<class> // Derived<E> is only only parameter
struct X; // but you actually need two parameters, Derived and E
template< template <class> class Derived, class E >
struct X< Derived< E > > // so use partial specialization.
{
static void f()
{
Derived<E>::value; // only reason to want this is to
Derived<int>::value; // use different specializations
}
};
当然,如果你不需要重新专攻Derived< something_else >
, 忽略 Derived<E>
的事实是模板特化:
template<class Derived>
struct X
{
static void f()
{
Derived::value;
}
};
X< my_class< something > > my_x; // a specialized class template is a class
关于c++ - 无权访问模板模板参数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/4293365/