I know questions about the syntax of template default arguments have been asked alot.
Usually, the answer (in sync with my understanding of how it should work) is to use something like that:
template <class T = SomeDefault> class T1 {};
Recently I wanted to check which map implementation Boost uses in its mapped_vector
. And found the following snippet:
template<class T, class A>
class mapped_vector:
Apparently, there is no default binding for the argument A
, but also apparently, I can instantiate a mapped_vector<int>
just fine. Obviosuly a default argument is inferred somehow, but how?
edit: Just to be precise, I am talking about line 279 in this file
The line here (@Xeo's link) declares the template. The line in your link defines it. Note : you can't specify a default again in the definition, if you decided to have a declaration.
This works: (Boost's version)
template<typename T = int> class A;
template<typename T> class A {};
This won't work:
template<typename T = bool> class A;
template<typename T = int> class A {};
This neither:
template<typename T = int> class A;
template<typename T = int> class A {};
Note that you have to declare it in either one. This works:
template<typename T> class A;
template<typename T = int> class A {};
Although a default can only be declared once, they don't have to be all declared in the same part. This works, but please don't do that:
template<class T, class U = bool> class A;
template<class T = int, class U> class A {};
... With no limit, really. This works: (blame - or thank - @Xeo)
template<class T, class U, class V = double> class A;
template<class T, class U = bool, class V> class A;
template<class T = int, class U, class V> class A {};
And of course, you're not forced to include a declaration. This works:
template<typename T = int> class A {};
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