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 type’s template parameter like this?
(I find it interesting that I’m not allowed to do the same in the out-of-line 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 is that it was proposed in comments on this answer that GCC may be in error here.
I will put a mail copy of a possible DR I recently sent out on Christmas here
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
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). AfterA::A, name lookup is complete and yields the constructor. The<int>can then only be parsed as a template argument list. If there is no template among your constructors, your code will be invalid.