模板参数的一种可能形式是类模板。 C++ 标准 (C++2003) 声明模板实例化期间模板模板参数的参数是“id 表达式”。这个非终端相当广泛。它允许析构函数、重载运算符等。例如,以下代码应该可以正常编译:
template <template <typename x> class T>
struct MyClass
{
T<int> a;
T<double> b;
};
template <typename x> struct Helper
{
~Helper() { }
x operator+(x p) { return(x[1]+p); }
x[4] c;
};
MyClass<Helper> p1;
MyClass<~Helper> p2;
MyClass<Helper::operaror+> p3;
最后两行没有任何意义。但从语法的角度来看,它们很好。语法没有(也不应该)准确地描述语言,但是第 14.3.3 段“模板模板参数”没有提到任何对这种情况下的语法规则的限制。
任何人都可以接受或反驳我的陈述吗:
- 模板模板参数只能是一个标识符,可能是合格的。
- 如果第一点是正确的,那绝对值得在标准中提及。
最佳答案
14.3 [temp.arg] p1
"There are three forms of template-argument, corresponding to the three forms of template-parameter: type, non-type and template. The type and form of each template-argument specified in a template-id shall match the type and form specified for the corresponding parameter declared by the template in its template-parameter-list.
参数~Helper
模板模板参数的类型不正确 template<typename> class T
,它不是类模板。
14.3.3 [temp.arg.template] p1
A template-argument for a template template-parameter shall be the name of a class template, expressed as id-expression.
~Helper
不是类模板的名称。
这很清楚地排除了您的示例。
关于c++ - 模板实例化中模板模板参数的可能形式,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11060208/