Difference between template name and template id

后端 未结 3 547
暖寄归人
暖寄归人 2020-12-09 12:49

C++ Standard

Section 14/2 :

In a function template declaration, the declarator-id shall be a template-name (i.e.,

3条回答
  •  春和景丽
    2020-12-09 13:25

    A declarator-id is the syntactical element that specifies the name in a simple-declaration ("type name;"). In the following "A" and "B::C" is the declarator-id

    int A;
    int B::C;
    int A();
    int *A;
    int A[42];
    template void A();
    

    A type-id syntactically is roughly a simple-declaration where the declarator-id is missing. A type-id is used as the syntactical element in a template type argument and in a cast.

    int // type-id
    int* // type-id
    int[] // type-id
    int() // type-id
    int(*)() // type-id
    

    A template-name is the name of a template. Syntactically it appears before a template-argument list. The above quote misuses "template-name" and "declarator-id", because a template-name is a plain identifier and does not contain any qualifiers. C++0x has changed the text to

    In a function template declaration, the last component of the declarator-id shall be a template-name or operator-function-id (i.e., not a template-id).

    (The last part appears in cases such as operator+()). Even the C++0x text misses some cases - see this defect report.

    The misuse of "declarator-id" happens in the note. The note was replaced by C++0x with

    [ Note: in a class template declaration, if the class name is a ... — end note ]

    In class template declarations, the name specified syntactically is a class-name instead of a declarator-id. The relation of class-name and declarator-id is as follows (very simplified...)

    class class-name { ... } declarator-id;
    class foo        { ... } bar;
    

    In class template declarations, there may not be a declarator-id specified.


    A template-id is a template-name followed by a template-argument list.


    The quote means that in a function template declaration, the name must not be a template-id. In your example you declare a function instead of a template. There are still cases where an explicit specialization declares a template, though. But that can only happen for member function templates

    template
    struct A {
      template
      void f();
    };
    
    // this explicit specialization *contains* a template declaration and
    // declares an identifier (a template-name) qualified by A:: 
    template<> template 
    void A::f() { }
    

提交回复
热议问题