c++ - 模板实例化中模板模板参数的可能形式

标签 c++ template-templates

模板参数的一种可能形式是类模板。 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 段“模板模板参数”没有提到任何对这种情况下的语法规则的限制。

任何人都可以接受或反驳我的陈述吗:

  1. 模板模板参数只能是一个标识符,可能是合格的。
  2. 如果第一点是正确的,那绝对值得在标准中提及。

最佳答案

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/

相关文章:

c++ - 模板模板参数不可见是否有解决方法?

c++ - double 会在算术表达式中隐式转换为 unsigned int 吗?

c++ - 如何在 C++ 中的列表末尾推断出模板参数?

c++ - 双链表 - 添加/显示

c++ - 如何就地扩展分配的内存

c++ - 模板模板参数有哪些用途?

c++ - 当返回值是class或class<class>或class<class, class>等时如何使用enable_if?

c++ - 如何从 boost::enable_shared_from_this 派生模板模板类?

c++ - STL vector 多线程

c++ - 为什么仅添加 cout 行后输出无法显示变量的内容?