 |
C++Talk.NET C++ language newsgroups
|
| View previous topic :: View next topic |
| Author |
Message |
Christoph Rabel Guest
|
Posted: Thu Aug 21, 2003 5:18 am Post subject: Why ist there no const version of operator[] for map? |
|
|
Hi!
I wonder where there is no const counterpart to the existing version.
I recently stumbled on that problem when writing a getValue(Key)
Function that I declared as const. I couldnt use operator[] to get the
value but had to use find().
Is there any harm in adding this function?
thx for any insights,
Christoph
---
[ 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.jamesd.demon.co.uk/csc/faq.html ]
|
|
| Back to top |
|
 |
James Dennett Guest
|
Posted: Thu Aug 21, 2003 9:31 am Post subject: Re: Why ist there no const version of operator[] for map? |
|
|
Christoph Rabel wrote:
| Quote: | Hi!
I wonder where there is no const counterpart to the existing version.
I recently stumbled on that problem when writing a getValue(Key)
Function that I declared as const. I couldnt use operator[] to get the
value but had to use find().
Is there any harm in adding this function?
thx for any insights,
|
This has been covered a number of times in the past;
a search of the archives through Google Groups might
well dig up repeated discussions.
In a nutshell:
(a) There is none because the convenience of having
operator[] automatically add values if they are absent
was chosen
(b) It is unacceptable to many for a const version to
behave differently than a non-const version, except in
const-ness, so a const operator[] cannot be added.
You can write a function to get an element from a map,
but you'll need to decide what it should do if the element
is absent. You'll just have to choose a name for the
function: the name operator[] has already been taken for
a function with different semantics.
-- James.
---
[ 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.jamesd.demon.co.uk/csc/faq.html ]
|
|
| Back to top |
|
 |
|
|
You cannot post new topics in this forum You cannot reply to topics in this forum You cannot edit your posts in this forum You cannot delete your posts in this forum You cannot vote in polls in this forum
|
|