[Bug libstdc++/41803] Compiler forcing iterator to const

chris at bubblescope dot net gcc-bugzilla@gcc.gnu.org
Thu Oct 22 23:14:00 GMT 2009



------- Comment #4 from chris at bubblescope dot net  2009-10-22 23:14 -------
The reason that in general changing the Point a std::multiset<Point>::iterator
refers to is forbidden is that doing so will break the invariant on which the
multiset is based, resulting in random crashes occuring later on.

The standard does say, in 23.1.4 paragraph 5 "Keys in an associative container
are immutable.". Seeing as you know that actually your changes won't break the
ordering, what you are doing is legal, and can be done in two ways:

1) Use mutable, as you were already suggesting.
2) Add a const_cast to the 'const point&' you get out.

I realise this is a little annoying, but it was considered the alternative was
too buggy, as most people would just changes things in (multi)sets without
realising in most cases it would lead to horribly broken code.


-- 

chris at bubblescope dot net changed:

           What    |Removed                     |Added
----------------------------------------------------------------------------
                 CC|                            |chris at bubblescope dot net


http://gcc.gnu.org/bugzilla/show_bug.cgi?id=41803



More information about the Gcc-bugs mailing list