Is it valid to write the template type's parameter list in a constructor declaration?

后端 未结 1 890
失恋的感觉
失恋的感觉 2021-02-09 15:10

Old GCC 4.1.2 accepts, and new GCC 4.5.1 accepts, the following program.

But is it actually correct? What does the standard say about declaring a constructor with the ty

1条回答
  •  终归单人心
    2021-02-09 15:43

    I will put a mail copy of a possible DR I recently sent out on Christmas here

    Is the following code well formed?

    template
    struct A {
      A();
    };
    

    The several compilers that I tested (clang, g++ and comeau conline) accept this. Indeed 12.1 does not forbid this (A is a name of that class and is not a typedef-name), but 8.3p1 says

    An unqualified-id occurring in a declarator-id shall be a simple identifier except for the declaration of some special functions (12.3, 12.4, 13.5) ...

    A constructor is a special member function, but the list of cross references does not include 12.1. Does that mean that the above code is ill-formed? Or is this an accidental omission?

    If you do the same in an out-of-line definition, you will try to pass template arguments to a constructor. This is valid code

    struct A {
      template A();
    };
    
    template<> A::A() { }
    

    The spec says that when the injected class name is used in a qualified name when looking into the scope of the class (just as in A::A), then when name lookup accepts function/constructor names, the injected class name reference will be translated to be resolved to the constructor(s) of that class (if the name lookup context only accepts types, then the name will remain the injected class name, and will denote the class type). After A::A, name lookup is complete and yields the constructor. The can then only be parsed as a template argument list. If there is no template among your constructors, your code will be invalid.

    0 讨论(0)
提交回复
热议问题