Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What is difference between const and non const key?

What is the difference between the following two lines?

map<int, float> map_data; map<const int, float> map_data; 
like image 252
NullPoiиteя Avatar asked Jul 14 '13 09:07

NullPoiиteя


People also ask

What is non const?

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.

What is non const reference?

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.

What is the difference between const reference and reference?

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.

Can a const function call a non const function?

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.


2 Answers

  • 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 constify 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.

like image 105
Lightness Races in Orbit Avatar answered Oct 20 '22 12:10

Lightness Races in Orbit


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.


Edit:

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.

like image 42
Maroun Avatar answered Oct 20 '22 12:10

Maroun