C++Talk.NET Forum Index C++Talk.NET
C++ language newsgroups
 
Archives   FAQFAQ   SearchSearch   MemberlistMemberlist   UsergroupsUsergroups   RegisterRegister 
 ProfileProfile   Log in to check your private messagesLog in to check your private messages   Log inLog in 

Why ist there no const version of operator[] for map?

 
Post new topic   Reply to topic    C++Talk.NET Forum Index -> C++ language, library and standards
View previous topic :: View next topic  
Author Message
Christoph Rabel
Guest





PostPosted: Thu Aug 21, 2003 5:18 am    Post subject: Why ist there no const version of operator[] for map? Reply with 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,

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





PostPosted: Thu Aug 21, 2003 9:31 am    Post subject: Re: Why ist there no const version of operator[] for map? Reply with quote



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
Display posts from previous:   
Post new topic   Reply to topic    C++Talk.NET Forum Index -> C++ language, library and standards All times are GMT
Page 1 of 1

 
Jump to:  
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


Powered by phpBB © 2001, 2006 phpBB Group
SEO toolkit © 2004-2006 webmedic.