Re: Is there a proposal to fix the operator[] (rvalue/lvalue issues)??
jose.diego@gmail.com wrote:
peter koch wrote:
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.
a value_type::second_type default object :-)
of course it can't be done. there is the reason I am asking if
there is a proposal for fixing that
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?)
I think it is a major defect in std::map.
It should return 0, of course - a temporary
map::value_type::second_type()
Why?
The problem, of course, is that operator[] returns a reference.
What would the reference refer to? It can't be a temporary.
One possibility would be to overload with an operator[]() const,
which returned a value.
as an example VS 2005 gives me that:
1>...blablabla.cpp(15) : error C2678: binary '[' : no operator found
which takes a left-hand operand of type 'const
Parameter::ParametersMap' (or there is no acceptable conversion)
Which is correct. In std::map, you cannot use operator[] on a
const map. And I agree that it is rather a pain. In my own,
pre-standard hash maps, operator[] returned a pointer, which was
null if the element wasn't present. Alternatively, you could
use an exception. Or even provide a contains function and make
it a fatal error to use operator[] with a key where contains
returns false.
--
James Kanze GABI Software
Conseils en informatique orient9e objet/
Beratung in objektorientierter Datenverarbeitung
9 place S9mard, 78210 St.-Cyr-l'cole, France, +33 (0)1 30 23 00 34
---
[ 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 ]