Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do default template arguments work?

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

like image 488
choeger Avatar asked Mar 19 '23 07:03

choeger


1 Answers

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 {};
like image 76
Ven Avatar answered Apr 01 '23 03:04

Ven