Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

value_type for a map having pointers as key

Tags:

c++

map

stl

For what I know, C++ defines map<a,b>::value_type as pair<const a,b>

What will happen if I use a pointer type as key type in map, i.e., is

std::map<const char*,int>::value_type::first_type = const char*

as I would expect from definition above or

std::map<const char*,int>::value_type::first_type = const char* const

as would be more logical (since otherwise i would be allowed to change key value from a map iterator)?

like image 886
pqnet Avatar asked Oct 17 '11 14:10

pqnet


2 Answers

Your reasoning is correct, value_type::first would be char const * const.

There is a common source of confusion in thinking that const T when T is a type * is const type *, but that is not so. Unlike macros, typedefs are not text substitution, nor are template arguments. When you do const T, if T is a typedef or template argument, you are adding a const to the type as a whole.

That is the one reason why I like to write my consts at the right of the type, as it causes less confusion: T const *, add an extra const, get T const * const.

like image 158
David Rodríguez - dribeas Avatar answered Nov 09 '22 22:11

David Rodríguez - dribeas


If a is const char*, then const a is indeed const char* const.

like image 41
K-ballo Avatar answered Nov 09 '22 23:11

K-ballo