Reputation: 309
Class template for map is like this
template < class Key, // map::key_type
class T, // map::mapped_type
class Compare = less<Key>, // map::key_compare
class Alloc = allocator<pair<const Key,T> > // map::allocator_type
> class map;
From the template it appears to take 4 template parameters but while declaring a map, I see only two template parameters.
map <int, int> table;
Is this not a problem? If its not is this correct to just define the type of last two(of the four)?
map<,,int,int>
Upvotes: 2
Views: 247
Reputation: 172924
- Is this not a problem?
This is not a problem, it's default template arguments.
Default template arguments are specified in the parameter lists after the = sign.
It means when you doesn't specify them, such as map<key_type, value_type>
, the default arguments will be used instead. i.e. Compare
will be less<key_type>
, and Alloc
will be allocator<pair<const key_type, value_type>>
.
- is this correct to just define the type of last two(of the four)? map<,,int,int>
No. Default arguments are used in place of the missing trailing arguments. You can't just specify the last two arguments, without specifying the first two and want the default arguments to be applied. And in this case the first two parameters don't have default argument at all.
BTW
If the default is specified for a template parameter of a primary class template, each subsequent template parameter must have a default argument, except the very last one may be a template parameter pack.
Upvotes: 7
Reputation: 410
This is the default parameter for the template.
But only one or more in the end could be the default parameter(i.e. once a default parameter exists, all of the parameters after this parameter must be the default parameter.)
so, your code like this :
map<,,int,int>
will be wrong.
the right code like this:
map<int,int>;
note: you can refer to Thinking.In.C++,.Second.Edition.Volume.2
Upvotes: 2
Reputation: 1
Is this not a problem?
No, that's what the default type definitions are for: = less<Key>
and = allocator<pair<const Key,T> >
.
As you can see these types can be deduced from Key
and T
.
If its not is this correct to just define the type of last two(of the four)?
map<,,int,int>
No that can't be done.
Upvotes: 5