Re: Is there a proposal to fix the operator[] (rvalue/lvalue issues)??
Hi Folks,
I agree with Peter, but let consider some comments that I've found
reading some books...
Stroustrup says:
"Subscripting a m a p adds a default element when the key is not found.
Therefore, there is no version of o p e r a t o r []() for c o n s t m
a p s. Furthermore, subscripting can be used only if the m a p p e d _
t y p e (value type) has a default value. If the programmer simply
wants to see if a key is present, the f i n d () operation (?17.4.1.6)
can be used to locate a k e y without modifying the m a p ." (The C++
Programming Language, p.482)
Josuttis says:
"Associative containers don't typically provide abilities for direct
element access. Instead, you must use iterators. For maps, however,
there is an exception to this rule."
...
"The advantage is that you can insert new elements into a map with a
more convenient
interface"
"The disadvantage is that you might insert new elements by accident or
mistake."
(The C++ Standard Library A Tutorial and Reference, p.205)
And also Meyers has comments about that:
"The operator[] function for maps is a novel creature, unrelated to the
operator[] functions for vectors, deques, and strings and equally
unrelated to the built-in operator[] that works with arrays. Instead,
map::operator[] is designed to facilitate "add or update"
functionality."
"It would be nice if the STL provided a function that offered the best
of both worlds, i.e., efficient add-or-update functionality in a
syntatically attractive package."
(Effective STL, p.107)
So I think that subscript operator was not originally conceived for
"travelling around" the map elements, and that would be a semantic
problem. However, changing map::operator[]() behavior would be a good
idea, I think....
peter koch escreveu:
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
template<typename MapType>
const MapType::value_type::second_type get(const MapType map, const
MapType::value_type::first_type value) {
MapType::const_iterator it = map.find(value);
if( it == map.end() ) return MapType::value_type::second_type();
return *it;
}
Your code seems incorrect : the typename keyword is mandatory for
accessing MapType::value_type::second_type and
MapType::value_type::first_type (dependent names), and you pass map by
value (very inefficient).
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.
Another interpretation is how operator[] works on constant maps:
#include <map>
int main()
{
const map<int,int> test;
return test[0];
}
Is this program ill-formed or will it return 0? I havent got the
standard in my hands, but my implementation tells me it is ill-formed.
But my gut feeling is that this should be well-formed and legal.
(Well... why shouldn't it?)
/Peter
---
[ 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 ]
---
[ 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 ]