Immutable set-keys in c++0x, where?
I heard in a talk that the keys in C++0x associative containers are no longer mutable. Before, in C++03, they have been mutable, and only the Standard's text sai开发者_运维百科d, that the order or keys must stay the same.
Now, luckily, this is illegal:
std::set<int> ss { 2,5,1,6,8,5,8,0,2,4,9 };
auto it = ss.find(4);
*it = 7; // 'ERROR: assignment of read-only location'
Where is this change reflected in C++0x? I looked Final Draft, but still see that find()
and such returns iterator
-- that sounds modifiable. (And why this is and must be I gan guess: containers like map
want to allow the value beeing modified. What exacly changed to make the key-part const?)
For your information, 23.2.4/5 in N3290 says:
For
set
andmultiset
the value type is the same as the key type. Formap
andmultimap
it is equal topair<const Key, T>
. Keys in an associative container are immutable.
and 23.2.4/6 says:
For associative containers where the value type is the same as the key type, both
iterator
andconst_iterator
are constant iterators.
...
Note:iterator
andconst_iterator
have identical semantics in this case
Does this quote solve your question?
Basically, iterator
and const_iterator
are now specified to be the same. That's the lowdown, not the technicalities, of course, but it's perfectly legal for set
containers to typedef iterator
as const_iterator
in C++0x.
For map
, of course, then it was a pair<const K, V>
, so it never had a problem.
精彩评论