clang++ - treat template class name as template in the class scope

后端 未结 2 1975
谎友^
谎友^ 2021-01-19 09:44

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,

2条回答
  •  一向
    一向 (楼主)
    2021-01-19 09:54

    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 
    class B {
        A< ::B> member; // whitespace required to prevent digraph; see comments
    };
    

    C++11

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

    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 <>.

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

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

    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.

提交回复
热议问题