Narek
Narek

Reputation: 39871

std::map with const template and const reference template parameters

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

Answers (1)

ForEveR
ForEveR

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 be CopyInsertable and mapped_type shall be DefaultInsertable into *this.

But, const reference is not CopyInsertable.

Upvotes: 2

Related Questions