I saw this answer to a question on SO related to the declaration for a default constructor of a class template that said that the following code is not valid C++ due to CWG1435:
template <class T> class Stack {
public:
Stack<T>(); //IS THIS VALID?
};
While another answer said that the above example is valid C++. There are 2 sources for the claim that the above example is valid:
Otherwise, it is treated as a type-name, and is equivalent to the template-name followed by the template-parameters of the class template enclosed in <>
- In a CppCon conference Dan Saks basically showed a very similar example.
So as we can see the two linked answers make opposite claims and i don't know which one is correct. So my question is which of the two answers is correct. That is, is the declaration Stack<T>();
valid C++ or not.
PS: I am asking my question for Modern C++ meaning from C++11 & onwards.
The shown snippet is valid for Pre-C++20 but not valid from C++20 & onwards as explained below.
Pre-C++20
From class.ctor#1.2:
(end quote)
This means that in C++17, we are allowed to use
Stack<T>();
as the constructor declaration.C++20
From class.ctor#1.1:
So as we can see, the injected class name(which is
Stack
and notStack<T>
in your example) is needed for declaring the ctor of a class template.This means that your given code is not valid for C++20.
The same is also mentioned at diff.cpp17.class#2: