Re: Is there a proposal to fix the operator[] (rvalue/lvalue issues)??
peter koch wrote:
SuperKoko wrote:
jose.diego@gmail.com wrote:
I made the following function to access a map elements to simulate the
operator[] in a const map
[...]
If the std::map<>::operator[] could be used as an rvalue, it could have
the above implementation
But... std::map<>::operator[] CAN be used as an rvalue (it can even be
used as an lvalue).
What do you think?
I don't see your point. Could you be more specific.
Perhaps Jose thought that operator[] could simply return a
value_type::first_type in those situations where the result is used as
a rvalue. This can not be done since there is no overload on
returnvalue.
But it is possible to overload on the const-ness of the operator[]. For
example, std::map's operator[] is currently declared:
T& operator[](const key_type& x);
But we could overload operator[] (in very much the same way that
std::string does) in order to support indexed access to both const and
non-const std::maps:
T& operator[](const key_type& x);
T operator[](const key_type& x) const;
Note that this change would have the nice effect of not breaking any
existing code. Instead, the second operator[] would enable a program to
use indexed access in order to access the values stored in a const map.
Now, there is the question of what would happen if the key index is not
found in a const map. The problem is how to communicate this failure to
the program. Because any value returned could have been one stored in
the map. Moreover, the value returned in this situation would not have
been added to the map (since it is const), thereby further misleading
the program. For these reasons, a const map would have to throw an
exception if operator[] is called with a key not present in the map.
Greg
---
[ comp.std.c++ is moderated. To submit articles, try just posting with ]
[ your news-reader. If that fails, use mailto:std-c++@ncar.ucar.edu ]
[ --- Please see the FAQ before posting. --- ]
[ FAQ: http://www.comeaucomputing.com/csc/faq.html ]