What is the difference between the following two lines?
map<int, float> map_data; map<const int, float> map_data;
You can always provide a non-const value where a const one was expected. For instance, you can pass non-const variables to a function that takes a const argument. The const-ness of the argument just means the function promises not to change it, whether or not you require that promise.
Whether a reference refers to a const or nonconst type affects what we can do with that reference, not whether we can alter the binding of the reference itself." I think this means that making a reference a "const" when it is referenced to a non const object does absolutely nothing.
A const reference is actually a reference to const. A reference is inherently const, so when we say const reference, it is not a reference that can not be changed, rather it's a reference to const. Once a reference is bound to refer to an object, it can not be bound to refer to another object.
const member functions may be invoked for const and non-const objects. non-const member functions can only be invoked for non-const objects. If a non-const member function is invoked on a const object, it is a compiler error.
int
and const int
are two distinct types.
std::map<int, float>
and std::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.
The key is already const
, so it is redundant to write const
in this case. Once an element is entered, its key
cannot be changed.
As mentioned in the comments, there is difference between the two lines. For example, if you write a function that accepts map<const int, int>
, you can't pass to it map<int, int>
since they're different types.
But note that although they are different types, they behave the same since the key in a map is a const
anyway...
So in conclusion.. The only difference is that they are two different types, you shouldn't care about anything else.
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