I was wondering whether this code is legal (in C++17, of course):
template<typename T = int>
class C {};
int main() {
C c;
}
This compiles fine with GCC 8.1.0 but fails with Clang 6.0.0. I tried to find an answer in the Standard, but I'm no language lawyer and I couldn't figure it out. On the other hand, cppreference states this:
std::less l; // same as std::less<void> l;
So, is it possible to define an object, whose type is a template class where all template parameters have got a default, without using the angular brackets?
EDIT
I try to clarify my doubt: my code, with a slight edit, i.e. changing the main
's body to
C<> c;
is legal code from C++98. Where, in the C++17 Standard, we may argue that the empty angular brackets are no more mandatory in this case?
So, is it possible to define an object, whose type is a template class where all template parameters have got a default, without using the square brackets?
The short answer is yes. The whole mechanism is similar (and in fact is based on) function template argument deduction. When deducing the template arguments of a class template, the deduction guides (both user-provided and compiler generated) are considered through an overload resolution process similar to what we have for function calls.
So you can do what you want, for the same reason this is valid:
template<typename T = void>
void foo() {}
int main() {
foo();
}
We call foo
without angle brackets, and T
is void
since it isn't deduced and has to be taken from the default argument.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With