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 

The issue of const maps and operator[]

 
Post new topic   Reply to topic    C++Talk.NET Forum Index -> C++ language, library and standards
View previous topic :: View next topic  
Author Message
Ney André de Mello Zunino
Guest





PostPosted: Thu Dec 02, 2004 5:38 am    Post subject: The issue of const maps and operator[] Reply with quote



Hello.

Searching around before posting this message revealed that the issue has
been brought up a significant number of times (here are two related
threads [1] [2]). So /std::map<>/ does not provide a const version of
its /operator[]/ member function because that function is supposed to
modify the map in case the passed key is not present. The question which
follows is: why not provide a const overload which would throw when the
given key would not exist? It seems like a natural and easy-to-implement
solution which would contribute to map's associative array role.

In spite of having found several discussions on this topic, I could not
find mention of whether this issue is going to be addressed when C++
gets its next major revision. Could anyone provide some information
(comments, links, etc.) on this?

[1] http://groups.google.com/groups?hl=en&lr=&th=47b92be5c9ed558c&rnum=1
[2] http://groups.google.com/groups?hl=en&lr=&th=698e93dddf8b22d0&rnum=5

Thank you very much,

--
Ney André de Mello Zunino

---
[ 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 Dec 02, 2004 6:51 am    Post subject: Re: The issue of const maps and operator[] Reply with quote



Ney André de Mello Zunino wrote:

Quote:
Hello.

Searching around before posting this message revealed that the issue has
been brought up a significant number of times (here are two related
threads [1] [2]). So /std::map<>/ does not provide a const version of
its /operator[]/ member function because that function is supposed to
modify the map in case the passed key is not present. The question which
follows is: why not provide a const overload which would throw when the
given key would not exist?

Because it would violate the important principle that adding
const to code should not change its behaviour (except for
propogation of constness to other code), although it may
cause it to no longer compile. The language doesn't enforce
that in isolation; good library design is also necessary.

Quote:
It seems like a natural and easy-to-implement
solution which would contribute to map's associative array role.

But it's too fragile, for the reason above.

Quote:
In spite of having found several discussions on this topic, I could not
find mention of whether this issue is going to be addressed when C++
gets its next major revision.

I find it enormously unlikely that this would be changed in the
next revision of C++.

-- 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
Thorsten Ottosen
Guest





PostPosted: Fri Dec 03, 2004 2:54 am    Post subject: Re: The issue of const maps and operator[] Reply with quote



"James Dennett" <jdennett (AT) acm (DOT) org> wrote


Quote:
In spite of having found several discussions on this topic, I could not
find mention of whether this issue is going to be addressed when C++
gets its next major revision.

I find it enormously unlikely that this would be changed in the
next revision of C++.

yes true. but we are already looking into adding

T& map<K,T>::at( const Key& );
const T& map<K,T>::at( const Key& ) const;

which should throw if nothing is found.
Alternatively, I guess we could use operator()().

-Thorsten


---
[ 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: Fri Dec 03, 2004 4:19 pm    Post subject: Re: The issue of const maps and operator[] Reply with quote

Thorsten Ottosen wrote:

Quote:
"James Dennett" <jdennett (AT) acm (DOT) org> wrote in message
news:YWxrd.191852$hj.44035 (AT) fed1read07 (DOT) ..


In spite of having found several discussions on this topic, I could not
find mention of whether this issue is going to be addressed when C++
gets its next major revision.


|I find it enormously unlikely that this would be changed in the
|next revision of C++.

yes true. but we are already looking into adding

T& map<K,T>::at( const Key& );
const T& map<K,T>::at( const Key& ) const;

This is reasonable and somewhat consistent with the at
member in other standard library class templates.

Quote:
which should throw if nothing is found.
Alternatively, I guess we could use operator()().

I wouldn't like that:
mymap[1] = 3;
and
mymap(1) = 3;
would then both be valid, with different semantics in
the case mymap.find(1) == mymap.end(); and identical
semantics otherwise, but they look too similar. Let's
be consistent, and use "at" when we mean range-checked
access which throws if an element is absent.

-- 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
Tom Widmer
Guest





PostPosted: Sat Dec 04, 2004 2:26 am    Post subject: Re: The issue of const maps and operator[] Reply with quote

On Fri, 3 Dec 2004 02:54:24 GMT, [email]nesotto (AT) cs (DOT) auc.dk[/email] ("Thorsten
Ottosen") wrote:

Quote:
"James Dennett" <jdennett (AT) acm (DOT) org> wrote in message
news:YWxrd.191852$hj.44035 (AT) fed1read07 (DOT) ..

In spite of having found several discussions on this topic, I could not
find mention of whether this issue is going to be addressed when C++
gets its next major revision.

|I find it enormously unlikely that this would be changed in the
|next revision of C++.

yes true. but we are already looking into adding

T& map<K,T>::at( const Key& );
const T& map<K,T>::at( const Key& ) const;

which should throw if nothing is found.

I like the symmetry with vector::at.

Quote:
Alternatively, I guess we could use operator()().

I don't like that, since operator() hasn't been used like that
elsewhere and is usually used for functors.

Tom

---
[ 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
John Nagle
Guest





PostPosted: Sat Dec 04, 2004 5:26 am    Post subject: Re: The issue of const maps and operator[] Reply with quote

If anything is done to "operator[]", it should be given
the same syntax as "operator()", so we can have classes that
support multiple subscripts. Inconsistently,

foo(a,b)

is a two-argument call, but

foo[a,b]

is an invocation of the comma operator followed by a
one-argument call. One of the more obscure features
of the language.

John Nagle
Animats

Tom Widmer wrote:

Quote:
On Fri, 3 Dec 2004 02:54:24 GMT, [email]nesotto (AT) cs (DOT) auc.dk[/email] ("Thorsten
Ottosen") wrote:


"James Dennett" <jdennett (AT) acm (DOT) org> wrote in message
news:YWxrd.191852$hj.44035 (AT) fed1read07 (DOT) ..


In spite of having found several discussions on this topic, I could not
find mention of whether this issue is going to be addressed when C++
gets its next major revision.

|I find it enormously unlikely that this would be changed in the
|next revision of C++.

yes true. but we are already looking into adding

T& map<K,T>::at( const Key& );
const T& map<K,T>::at( const Key& ) const;

which should throw if nothing is found.


I like the symmetry with vector::at.


Alternatively, I guess we could use operator()().


I don't like that, since operator() hasn't been used like that
elsewhere and is usually used for functors.

Tom

---
[ 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 ]


---
[ 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.