Reputation: 39871
Please see the code below. I have use there const template types. The first line compiles, the other two don't. Why those two don't compile? And the first one which compiles - is it OK to write it? What is the difference of std::map<const int, const bool>
and std::map<int, bool>
?
std::map<const int, const bool> mm;
std::map<const int&, const bool> mm;
std::map<const int, const bool&> mm;
I know that this is a strange question but please help to clarify it.
Upvotes: 1
Views: 943
Reputation: 55887
Why const
value? map::value_type
is really std::pair<const Key, Value>
.
You cannot store reference in any
containter.
One of requirements from standard.
T& operator[](const key_type& x);
Requires:
key_type
shall beCopyInsertable
and mapped_type shall beDefaultInsertable
into *this.
But, const reference
is not CopyInsertable
.
Upvotes: 2