Why can't we have an immutable version of operator[] for map
- by Yan Cheng CHEOK
The following code works fine :
std::map<int, int>& m = std::map<int, int>();
int i = m[0];
But not the following code :
// error C2678: binary '[' : no operator...
const std::map<int, int>& m = std::map<int, int>();
int i = m[0];
Most of the time, I prefer to make most of my stuff to become immutable, due to reason :
http://www.javapractices.com/topic/TopicAction.do?Id=29
I look at map source code. It has
mapped_type& operator[](const key_type& _Keyval)
Is there any reason, why std::map unable to provide
const mapped_type& operator[](const key_type& _Keyval) const