-
int
andconst int
are two distinct types. -
std::map<int, float>
andstd::map<const int, float>
are, similarly, different types.
The difference between std::map<const int, float>
and std::map<int, float>
is, to a degree, analogous to the difference between, say, std::map<int, float>
and std::map<std::string, float>
; you get a fresh map type for each.
In the non-const
case, the internal key type is still non-const
int
:
std::map<const int, float>::key_type => const int
std::map<int, float>::key_type => int
However, map keys are semantically immutable, and all map operations that allow direct access to keys (for example, dereferencing iterators, which yields value_type
) does const
ify the key_type
:
std::map<const int, float>::value_type => std::pair<const int, float>
std::map<int, float>::value_type => std::pair<const int, float>
So the difference may be largely invisible to you in every way that matters, if your implementation allows it.
That’s not always the case, though: the standard officially requires your key type to be copyable and moveable, and some implementations re-use map nodes; under those implementations, attempting to use a const
key simply won’t work.