clang ++-将模板类名称视为类范围中的模板 [英] clang++ - treat template class name as template in the class scope

查看:74
本文介绍了clang ++-将模板类名称视为类范围中的模板的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

似乎clang ++(我尝试过clang 3.2)将模板类的名称视为实例化的类,而不是用于在类范围内发生的任何模板。例如,以下代码

It seems that clang++ (I tried clang 3.2) treats the name of a template class as a instantiated class, not a template for any occurence within the class scope. For example, the following codes

template <template <class> class T>
class A {};

template <typename T>
class B {
    A<B> member;
   // ^---- clang++ treats B as an instantiated class
         // but I want it to be a template here
         // this code could compile in g++
};

int main()
{
    B<int> b;
    return 0;
}

我应该怎么做?

推荐答案

C ++ 03



解析 B 这种方式(称为 injected-class-name ,每个类的隐式声明成员,包括模板实例化)旨在提供便利。我从未见过这样的方式!

C++03

Resolving B that way (called the injected-class-name, an implicitly-declared member of every class including template instantiations) is intended as a convenience. I've never seen it get in the way like that!

要解决此问题,请添加 ::

To work around, qualify the name by adding :: before it (and if necessary, the name of the namespace).

template <typename T>
class B {
    A< ::B> member; // whitespace required to prevent digraph; see comments
};



C ++ 11



C + +11§14.6.1/ 1指定(强调我的意思)

C++11

C++11 §14.6.1/1 specifies (emphasis mine)


与普通(非模板)类一样,类模板也注入了-类名(第9条)。注入的类名可以用作模板名或类型名。当它与模板参数列表一起使用时,作为模板模板参数的模板参数或作为朋友类模板声明的详细类型说明符中的最终标识符, 它是指类模板本身。否则,它等效于<>中包含的类模板的模板名称和模板参数。

Like normal (non-template) classes, class templates have an injected-class-name (Clause 9). The injected- class-name can be used as a template-name or a type-name. When it is used with a template-argument-list, as a template-argument for a template template-parameter, or as the final identifier in the elaborated-type- specifier of a friend class template declaration, it refers to the class template itself. Otherwise, it is equivalent to the template-name followed by the template-parameters of the class template enclosed in <>.

因此,如果在C ++ 11下发生此问题,则是编译器错误。

Therefore, if this problem occurs under C++11 it is a compiler bug. Workaround as above.

注意—为了进行比较,C ++ 03中的相应段落是

Note — for comparison, the corresponding paragraph in C++03 is


类似于普通(非模板)类,类模板具有注入的类名(第9节)。注入的类名称可以与模板参数列表一起使用,也可以不与模板参数列表一起使用。当不使用模板参数列表使用它时,它等效于注入的类名称,后跟<>中包含的类模板的模板参数。当它与template-argument-list一起使用时,它指的是指定的类模板专业化,可以是当前专业化或另一个专业化。

Like normal (non-template) classes, class templates have an injected-class-name (clause 9). The injected- class-name can be used with or without a template-argument-list. When it is used without a template- argument-list, it is equivalent to the injected-class-name followed by the template-parameters of the class template enclosed in <>. When it is used with a template-argument-list, it refers to the specified class template specialization, which could be the current specialization or another specialization.

如您所见,已经有一种特殊情况,允许标识符是类还是模板,具体取决于它是否出现在模板名称中。他们只是增加了几个案例。

As you can see, there's already one special case allowing the identifier to be a class or template, depending on whether it occurs in a template-name. They just added a couple more cases.

这篇关于clang ++-将模板类名称视为类范围中的模板的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆