Is there a good reason why <>
is required when specifying a template class which has defaults for all its template parameters?
e.g.
#include <iostream>
template<typename T = int>
class C {
public:
T obj = 0;
};
int main()
{
C c1; // Error on almost all compilers (see note below)
C<> c2;
std::cout << c1.obj << " " << c2.obj << std::endl;
return 0;
}
An example disadvantage of this is that if you have a class which is already used in various places, and you later refactor it to be a class template with default parameters for its template arguments, then you have to add <>
in all the places which use the class.
Note: it looks like GCC latest HEAD (7.0.1) accepts the syntax without <>
. Earlier versions do not and neither does any version of Clang. Is this a bug in the latest GCC HEAD? Or perhaps C++17's standard now accepts the syntax without <>
and GCC is just ahead here?