Re: Is there a proposal to fix the operator[] (rvalue/lvalue issues)??
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 ]