Is it correct to write a list of template type parameters in the constructor declaration?

The old GCC 4.1.2 accepts , and the new GCC 4.5.1 accepts , the next program.

But is it really right? What does the standard say about a constructor declaration with a template type parameter like this?

(I am interested in not being allowed to do the same in an offline definition .)

#include <iostream> template <typename T> struct Foo { Foo<T>(); // <--- }; template <typename T> Foo<T>::Foo() { std::cout << ":)"; } int main() { Foo<int> f; } 

The reason I ask that it was suggested in the comments on this answer is that there might be a GCC error.

+6
source share
1 answer

I will put a mail copy of a possible DR that I recently posted on Christmas here

Is the following code formed correctly?

 template<typename T> struct A { A<T>(); }; 

Several compilers I tested (clang, g ++ and conau conline) Accept this. Indeed, 12.1 does not prohibit this ( A<T> is the name of this class and is not typedef-name), but 8.3p1 says

The unqualified identifier found in the declarator identifier should be a simple identifier, with the exception of the declaration of some special functions (12.3, 12.4, 13.5) ...

The constructor is a special member function, but the list of crosses doesnโ€™t include 12.1. Does this mean that the above code is poorly formed? Or is this an accidental omission?

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

 struct A { template<typename T> A(); }; template<> A::A<int>() { } 

The specification states that when the name of the entered class is used in the qualified name when searching in the class of the class (as in A::A ), then when the name search accepts the names of functions / constructors, a reference to the name of the entered class will be translated to be resolved to the constructor (s) of this class (if the name search context accepts only types, the name will remain the name of the entered class and will indicate the type of class). After A::A name search is complete and gives a constructor. Then <int> can only be parsed as a list of template arguments. If there is no template among your constructors, your code will be invalid.

+3
source

Source: https://habr.com/ru/post/904486/


All Articles