The issue of const maps and operator[]



 DEVELOP > c-Plus-Plus > The issue of const maps and operator[]

LINK TO THIS PAGE  


rating :  0   |  0


  Page 1 of 1

1

 
Topic: DEVELOP > c-Plus-Plus
User: "=?ISO-8859-1?Q?Ney_Andr=E9_de_Mello_Zunino?="
Date: 01 Dec 2004 11:38:47 PM
Object: The issue of const maps and operator[]
Hello.
Searching around before posting this message revealed that the issue has=20
been brought up a significant number of times (here are two related=20
threads [1] [2]). So /std::map<>/ does not provide a const version of=20
its /operator[]/ member function because that function is supposed to=20
modify the map in case the passed key is not present. The question which=20
follows is: why not provide a const overload which would throw when the=20
given key would not exist? It seems like a natural and easy-to-implement=20
solution which would contribute to map's associative array role.
In spite of having found several discussions on this topic, I could not=20
find mention of whether this issue is going to be addressed when C++=20
gets its next major revision. Could anyone provide some information=20
(comments, links, etc.) on this?
[1] http://groups.google.com/groups?hl=3Den&lr=3D&th=3D47b92be5c9ed558c&r=
num=3D1
[2] http://groups.google.com/groups?hl=3Den&lr=3D&th=3D698e93dddf8b22d0&r=
num=3D5
Thank you very much,
--=20
Ney Andr=E9 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 ]
.

User: "Daniel"

Title: Re: The issue of const maps and operator[] 02 Dec 2004 05:13:45 AM
Ney Andr=E9 de Mello Zunino wrote:

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.

C++ is powerful enough to make this an optional accessory, so it cannot
be used "by accident".
You can write a general wrapper for any map like this:
template <class M>
class const_map
{
const M &imp;
public:
const_map(const M &m) : imp(m) {}
const typename M::value_type::second_type &
operator[](const typename M::key_type &k) const
{
typename M::const_iterator f =3D imp.find(k);
if (f =3D=3D imp.end())
throw std::range_error("Not found");
return f->second;
}
};
It can be used as the argument type for a function that only needs to
look up items, and needs throwing semantics for items it can't find.
When a "naked" map is passed to such a function, the compiler
automatically wraps it in a temporary wrapper to impose those
semantics.
// details of a person
typedef std::map<std::string, std::string> person;
// function that wants read-only/throw semantics
void foo(const const_map<person> &p)
{
std::cout << p["name"] << std::endl;
std::cout << p["address"] << std::endl;
std::cout << p["phone"] << std::endl;
}
int main(int argc, char *argv[])
{
try
{
person p;
p["name"] =3D "Santa";
p["address"] =3D "North pole";
foo(p);
}
catch (std::exception &e)
{
std::cout << "Error: " << e.what() << std::endl;
return EXIT_FAILURE;
}
return EXIT_SUCCESS;
}
$ g++ cmap.cpp
$ ./a.out
Santa
North pole
Error: Not found
.

User: "James Dennett"

Title: Re: The issue of const maps and operator[] 02 Dec 2004 12:51:23 AM
Ney Andr=E9 de Mello Zunino wrote:

Hello.
=20
Searching around before posting this message revealed that the issue ha=

s=20

been brought up a significant number of times (here are two related=20
threads [1] [2]). So /std::map<>/ does not provide a const version of=20
its /operator[]/ member function because that function is supposed to=20
modify the map in case the passed key is not present. The question whic=

h=20

follows is: why not provide a const overload which would throw when the=

=20

given key would not exist?=20

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.

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

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

In spite of having found several discussions on this topic, I could not=

=20

find mention of whether this issue is going to be addressed when C++=20
gets its next major revision.=20

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 ]
.
User: "Thorsten Ottosen"

Title: Re: The issue of const maps and operator[] 02 Dec 2004 08:54:24 PM
"James Dennett" <jdennett@acm.org> wrote in message
news:YWxrd.191852$hj.44035@fed1read07...

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 ]
.
User: "Tom Widmer"

Title: Re: The issue of const maps and operator[] 03 Dec 2004 08:26:30 PM
On Fri, 3 Dec 2004 02:54:24 GMT,
("Thorsten
Ottosen") wrote:

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

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 ]
.
User: "John Nagle"

Title: Re: The issue of const maps and operator[] 03 Dec 2004 11:26:02 PM
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:

On Fri, 3 Dec 2004 02:54:24 GMT,

("Thorsten
Ottosen") wrote:


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


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


User: "James Dennett"

Title: Re: The issue of const maps and operator[] 03 Dec 2004 10:19:55 AM
Thorsten Ottosen wrote:

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


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.

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




  Page 1 of 1

1

 


Related Articles
 

NEWER

pg.1232     pg.940     pg.716     pg.544     pg.412     pg.311     pg.234     pg.175     pg.130     pg.96     pg.70     pg.50     pg.35     pg.24     pg.16     pg.10     pg.6     pg.3     pg.1

OLDER