Topic: Associative container const-correctness dichotomy


Author: "Leigh Johnston" <leigh@i42.co.uk>
Date: Sat, 15 Aug 2009 13:35:46 CST
Raw View
"Ron" <ron.natalie@gmail.com> wrote in message
news:3183b79f-f25e-4a2b-ab7f-b1abb96ee2ec@h21g2000yqa.googlegroups.com...
> On Aug 10, 2:53 am, Sean Hunt <ride...@gmail.com> wrote:
<snip>
> When inserting, the object is copied via a reference which is const.
> This
> enforces that set itself won't change the object being inserted.
>
<snip>

A copy is made yes but the requirement that you cannot change std::set
elements (the current wording in the standard implies this) makes std::set
next to useless as a container (it might as well have a const value_type).
Just because you make a copy of something (via a reference to const) does
not mean you cannot change that copy (to suggest this is always the case is
nonsense).

/Leigh


--
[ comp.std.c++ is moderated.  To submit articles, try just posting with ]
[ your news-reader.  If that fails, use mailto:std-c++@netlab.cs.rpi.edu]
[              --- Please see the FAQ before posting. ---               ]
[ FAQ: http://www.comeaucomputing.com/csc/faq.html                      ]





Author: Sean Hunt <rideau3@gmail.com>
Date: Mon, 10 Aug 2009 00:53:04 CST
Raw View
In a heated discussion in ##c++ on Freenode, the question has come up
as to the constness of set's keys. They seem to be specified in the
standard as nonconst and yet implemented in const in GCC and in
Comeau, but not in MSVC.

Arguments for both sides are present; the standard's definition of
"immutable" (which is what it specifies keys are specified to be) is
unclear. In the one case, const-correctness is good, but in the other,
it's nice to be able to edit parts of objects that have no relation to
the sort order without const_cast or the like.

What really confuses me personally is why set uses non-const
value_type and map uses const key_type in the value type (which is, of
course, actually a pair). Why is this?

Sean Hunt

--
[ comp.std.c++ is moderated.  To submit articles, try just posting with ]
[ your news-reader.  If that fails, use mailto:std-c++@netlab.cs.rpi.edu]
[              --- Please see the FAQ before posting. ---               ]
[ FAQ: http://www.comeaucomputing.com/csc/faq.html                      ]





Author: Ron <ron.natalie@gmail.com>
Date: Fri, 14 Aug 2009 12:45:12 CST
Raw View
On Aug 10, 2:53 am, Sean Hunt <ride...@gmail.com> wrote:

> What really confuses me personally is why set uses non-const
> value_type and map uses const key_type in the value type (which is, of
> course, actually a pair). Why is this?
>
It's not necessary nor sufficient to declare the type const in set's
interface.
In both the case of map and set, the stronger requirement that the
ordering
of the objects via the provided comparison function be immutable be
upheld.
When inserting, the object is copied via a reference which is const.
This
enforces that set itself won't change the object being inserted.

The use of the pair class in map is a sloppy convenience.   The const
on
the key is necessary there, but again not sufficient.



--
[ comp.std.c++ is moderated.  To submit articles, try just posting with ]
[ your news-reader.  If that fails, use mailto:std-c++@netlab.cs.rpi.edu]
[              --- Please see the FAQ before posting. ---               ]
[ FAQ: http://www.comeaucomputing.com/csc/faq.html                      ]