public inbox for gcc-prs@sourceware.org help / color / mirror / Atom feed
From: Mathias Hasselmann <mathias.hasselmann@gmx.de> To: nobody@gcc.gnu.org Cc: gcc-prs@gcc.gnu.org, Subject: Re: libstdc++/5583: std::set::iterator is readonly Date: Tue, 05 Feb 2002 15:36:00 -0000 [thread overview] Message-ID: <20020205233600.25362.qmail@sources.redhat.com> (raw) The following reply was made to PR libstdc++/5583; it has been noted by GNATS. From: Mathias Hasselmann <mathias.hasselmann@gmx.de> To: Carlo Wood <carlo@alinoe.com> Cc: "rodrigc@gcc.gnu.org" <rodrigc@gcc.gnu.org>, "gcc-bugs@gcc.gnu.org" <gcc-bugs@gcc.gnu.org>, "gcc-prs@gcc.gnu.org" <gcc-prs@gcc.gnu.org>, "nobody@gcc.gnu.org" <nobody@gcc.gnu.org>, "gcc-gnats@gcc.gnu.org" <gcc-gnats@gcc.gnu.org> Subject: Re: libstdc++/5583: std::set::iterator is readonly Date: Wed, 6 Feb 2002 00:29:00 +0100 (CET) On Tue, 5 Feb 2002, Carlo Wood wrote: > On Tue, Feb 05, 2002 at 02:56:09PM -0000, rodrigc@gcc.gnu.org wrote: > > Synopsis: std::set::iterator is readonly > > > > State-Changed-From-To: open->feedback > > State-Changed-By: rodrigc > > State-Changed-When: Tue Feb 5 06:56:09 2002 > > State-Changed-Why: > > Your testcase in the "How-to-Repeat" section is > > incorrect and doesn't even compile. > > Can you submit a full testcase (including #include statements) > > which illustrates your problem? > > > > http://gcc.gnu.org/cgi-bin/gnatsweb.pl?cmd=view%20audit-trail&database=gcc&pr=5583 > > I didn't look at the PR, but from the Subject I bet that > he means that (*iter) returns a constant object, even > for set<Non-Const-Type>::iterator. > > If so, then this is not a bug: the elements of set<> have to be > constant, you are not allowed to change them because that would > change the ordering in the set<>. Agreed: There are situations in which changing attributes of an element would change the ordering in the set. Looks like my acknowledgement of this situation was not clear enough in the report. BUT: Condisider the situation where I want to use std::set to store a set of edges of an directed weighted graph. Edges of a directed graph are triples of the kind (u,v,w) where u and v refer to vertices of the graph and w describes the weight of the edge. Obviously edges of a directed weighted exclusively are distinguished by their vertices. Obviously it would be nonsense to allow edgessets like {(1,2,3),(1,2,3)} or {(1,2,3),(1,2,inf)}. I hope you agree so far. Now since we have our nice set of edges: struct edge_t { vertex_t * u, * v; double weight; void set_weight(double new_weight) { weight = new_weight; } }; typedef std::set<edge_t> set_of_edges_t; we want to use this ADT to implement for instance Dijkstra's Shortest Path Algorithm. At the very beginning of Dijkstra's Algorithm (in the form I know it) we have to initialize all edges of the graph to have an infinite weight. In my opinion a good way to achieve this would be to write: set_of_edges_t edges; std::for_each(edges.begin(), edges.end(), std::bind2nd(std::mem_fun_ref(&edge_t::set_weight, INFINITY)); Unfortunatly this simple piece of code fails to compile when std::set::iterator is a read-only iterator. Well, it would be possible to hack arround the problem by changing the definition of edge_t: struct edge_t { vertex_t * u, * v; mutable double weight; // <- made it mutable void set_weight(double new_weight) const // <- const now { weight = new_weight; } }; But somehow I don't feel well when doing it that way: It's set_weight's purpose to modify edges contra caricatures the entire idea of the "const" keyword... In this situation both "const" qualifiers (the one of the iterator and the one of set_weight) are used in an absurd manner. Ciao, Mathias -- WWW: http://www.informatik.hu-berlin.de/~hasselma/ PGP/GnuPG: 1024-Bit DSA: ID 55E572F3, 1024-Bit RSA: ID EAAF7CF1
next reply other threads:[~2002-02-05 23:36 UTC|newest] Thread overview: 5+ messages / expand[flat|nested] mbox.gz Atom feed top 2002-02-05 15:36 Mathias Hasselmann [this message] -- strict thread matches above, loose matches on Subject: below -- 2002-03-11 14:12 pme 2002-02-05 7:26 Carlo Wood 2002-02-05 6:56 rodrigc 2002-02-04 11:06 mathias.hasselmann
Reply instructions: You may reply publicly to this message via plain-text email using any one of the following methods: * Save the following mbox file, import it into your mail client, and reply-to-all from there: mbox Avoid top-posting and favor interleaved quoting: https://en.wikipedia.org/wiki/Posting_style#Interleaved_style * Reply using the --to, --cc, and --in-reply-to switches of git-send-email(1): git send-email \ --in-reply-to=20020205233600.25362.qmail@sources.redhat.com \ --to=mathias.hasselmann@gmx.de \ --cc=gcc-prs@gcc.gnu.org \ --cc=nobody@gcc.gnu.org \ /path/to/YOUR_REPLY https://kernel.org/pub/software/scm/git/docs/git-send-email.html * If your mail client supports setting the In-Reply-To header via mailto: links, try the mailto: linkBe sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions for how to clone and mirror all data and code used for this inbox; as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).