Topic: std::set::at and operator[]
Author: David Krauss <potswa@gmail.com>
Date: Fri, 1 Aug 2014 10:41:27 +0800
Raw View
--Apple-Mail=_2E725726-97A4-434D-B7FC-2493B97E74BB
Content-Transfer-Encoding: quoted-printable
Content-Type: text/plain; charset=ISO-8859-1
Sometimes std::set is used as a substitute for std::map when the comparison=
data is contained in the stored item. Refactoring the code to convert the =
value_type of std::pair<T,U> into just a bigger U is unnecessarily difficul=
t though, and the resulting std::set usage may appear less elegant than std=
::map despite the design being actually cleaner without std::pair.
I don't see any reason why std::set shouldn't have an at member with the sa=
me semantics as map::at: dereference find if it does not return end, or thr=
ow out_of_range.
Analogously, operator[] should be defined to return a reference to the matc=
hing element in the set, or insert the given key otherwise.
The returned references would be non-modifiable. These methods would be sho=
rtcuts to the existing safe semantics, just the same as their map counterpa=
rts. If non-modifiable results are unintuitive to some, it's no worse than =
the status quo which is equally unintuitive and also inconvenient.
--=20
---=20
You received this message because you are subscribed to the Google Groups "=
ISO C++ Standard - Future Proposals" group.
To unsubscribe from this group and stop receiving emails from it, send an e=
mail to std-proposals+unsubscribe@isocpp.org.
To post to this group, send email to std-proposals@isocpp.org.
Visit this group at http://groups.google.com/a/isocpp.org/group/std-proposa=
ls/.
--Apple-Mail=_2E725726-97A4-434D-B7FC-2493B97E74BB
Content-Transfer-Encoding: quoted-printable
Content-Type: text/html; charset=ISO-8859-1
<html><head><meta http-equiv=3D"Content-Type" content=3D"text/html charset=
=3Dwindows-1252"></head><body style=3D"word-wrap: break-word; -webkit-nbsp-=
mode: space; -webkit-line-break: after-white-space;">Sometimes <font face=
=3D"Courier">std::set</font> is used as a substitute for <font face=3D"Cour=
ier">std::map</font> when the comparison data is contained in the stored it=
em. Refactoring the code to convert the <font face=3D"Courier">value_t=
ype</font> of <font face=3D"Courier">std::pair<T,U></font> into just =
a bigger <font face=3D"Courier">U</font> is unnecessarily difficult th=
ough, and the resulting <font face=3D"Courier">std::set</font> usage may ap=
pear less elegant than <font face=3D"Courier">std::map</font> despite the d=
esign being actually cleaner without <font face=3D"Courier">std::pair</font=
>.<div><br></div><div>I don’t see any reason why <font face=3D"Courie=
r">std::set</font> shouldn’t have an <font face=3D"Courier">at</font>=
member with the same semantics as <font face=3D"Courier">map::at</font>: d=
ereference <font face=3D"Courier">find</font> if it does not retu=
rn <font face=3D"Courier">end</font>, or throw <font face=3D"Courier">out_o=
f_range</font>.</div><div><br></div><div>Analogously, <font face=3D"Co=
urier">operator[]</font> should be defined to return a reference to the mat=
ching element in the set, or insert the given key otherwise.</div><div><br>=
</div><div>The returned references would be non-modifiable. These methods w=
ould be shortcuts to the existing safe semantics, just the same as their <f=
ont face=3D"Courier">map</font> counterparts. If non-modifiable results are=
unintuitive to some, it’s no worse than the status quo which is equa=
lly unintuitive and also inconvenient.</div><div><br></div></body></html>
<p></p>
-- <br />
<br />
--- <br />
You received this message because you are subscribed to the Google Groups &=
quot;ISO C++ Standard - Future Proposals" group.<br />
To unsubscribe from this group and stop receiving emails from it, send an e=
mail to <a href=3D"mailto:std-proposals+unsubscribe@isocpp.org">std-proposa=
ls+unsubscribe@isocpp.org</a>.<br />
To post to this group, send email to <a href=3D"mailto:std-proposals@isocpp=
..org">std-proposals@isocpp.org</a>.<br />
Visit this group at <a href=3D"http://groups.google.com/a/isocpp.org/group/=
std-proposals/">http://groups.google.com/a/isocpp.org/group/std-proposals/<=
/a>.<br />
--Apple-Mail=_2E725726-97A4-434D-B7FC-2493B97E74BB--
.