<div dir="ltr"><br><div class="gmail_extra"><br><br><div class="gmail_quote">On Fri, Nov 29, 2013 at 11:42 AM, Howard Hinnant <span dir="ltr"><<a href="mailto:hhinnant@apple.com" target="_blank">hhinnant@apple.com</a>></span> wrote:<br>
<blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex"><div class="HOEnZb"><div class="h5"><br>
On Nov 29, 2013, at 2:50 AM, Kal <<a href="mailto:b17c0de@gmail.com">b17c0de@gmail.com</a>> wrote:<br>
<br>
> Am 29.11.13 04:17, schrieb Howard Hinnant:<br>
>> On Nov 28, 2013, at 9:58 PM, Karen Shaeffer <<a href="mailto:shaeffer@neuralscape.com">shaeffer@neuralscape.com</a>> wrote:<br>
>><br>
>>> On Fri, Nov 29, 2013 at 02:19:44AM +0100, Kal wrote:<br>
>>>> Hi Howard, etc,<br>
>>>><br>
>>>> libc++3.4 (rc1/trunk) std::map doesn't support self-assignment for std <<br>
>>>> c++11. The relevant code is:<br>
>>>><br>
>>>>   _LIBCPP_INLINE_VISIBILITY<br>
>>>>   map& operator=(const map& __m)<br>
>>>>       {<br>
>>>> #if __cplusplus >= 201103L<br>
>>>>           __tree_ = __m.__tree_;<br>
>>>> #else<br>
>>>>           __tree_.clear();<br>
>>>>           __tree_.value_comp() = __m.__tree_.value_comp();<br>
>>>>           __tree_.__copy_assign_alloc(__m.__tree_);<br>
>>>>           insert(__m.begin(), __m.end());<br>
>>>> #endif<br>
>>>>           return *this;<br>
>>>>       }<br>
>>>><br>
>>>> Maybe should be like:<br>
>>>><br>
>>>>   _LIBCPP_INLINE_VISIBILITY<br>
>>>>   map& operator=(const map& __m)<br>
>>>>       {<br>
>>>> #if __cplusplus >= 201103L<br>
>>>>           __tree_ = __m.__tree_;<br>
>>>> #else<br>
>>>>           if (this != &__m) {<br>
>>>>               __tree_.clear();<br>
>>>>               __tree_.value_comp() = __m.__tree_.value_comp();<br>
>>>>               __tree_.__copy_assign_alloc(__m.__tree_);<br>
>>>>               insert(__m.begin(), __m.end());<br>
>>>>           }<br>
>>>> #endif<br>
>>>>           return *this;<br>
>>>>       }<br>
>>>><br>
>>>> I see the same issue with unordered_map& operator=(const unordered_map&<br>
>>>> __u).<br>
>>>><br>
>>>> Thanks!<br>
>>>> Kal<br>
>>> --- end quoted text ---<br>
>>><br>
>>> Hi Kal,<br>
>>> I don't speak for Howard or anyone else. But my understanding is the stl library<br>
>>> generally doesn't perform checks like that with the goal of best possible performance.<br>
>>> I see a lot of code in stdlibc++ just like that.<br>
>> Actually this does look like a bug to me (for  __cplusplus < 201103L).  I think Kal has the correct fix.  A post-condition of copy assignment is that both lhs and rhs should be equivalent to the previous value of rhs.  For move assignment this is not the case.  But this is copy assignment.<br>

>><br>
>> Howard<br>
>><br>
> Hi Howard,<br>
> Thanks for the reply. Do you think a fix for this will be committed for<br>
> the 3.4 release?<br>
<br>
</div></div>I don't know.<br>
<div class="im"><br>
><br>
> Also could you explain why the special check for __cplusplus < 201103L<br>
> is necessary at all. Both branches are doing almost the same thing<br>
> (looking into the implementation of __tree). Is there some subtle<br>
> difference in the standard that requires a different implementation here?<br>
<br>
</div>C++11 introduces the possibility of recycling nodes of [multi]map under the copy assignment operator by pushing the requirements on [multi]map::value_type down to the key_type and mapped_type (23.2.4 [associative.reqmts]/p7).  To actually implement this optimization I used new C++11 language features of union, which do not compile when -std=c++03.<br>

<br>
In C++11, using libc++, if two equal sized map<int, int> are assigned, there are zero trips to the heap.  The nodes in the lhs are assigned new values, and rebalancing takes place as necessary.</blockquote><div><br>
</div><div>In what situation would rebalancing need to happen during this operation? If they are the same size, wouldn't you just simultaneously inorder walk both, assigning element by element (or the equivalent)? The structure of an rb-tree is independent of the actual values contained.</div>
<div><br></div><div>-- Sean Silva</div><div> </div><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex">  In C++03, the lhs is first clear()'d, and then one does an insert for every value in the rhs.<br>

<br>
Same design (and bug as you note) in unordered_[multi]map.<br>
<span class="HOEnZb"><font color="#888888"><br>
Howard<br>
</font></span><div class="HOEnZb"><div class="h5"><br>
_______________________________________________<br>
cfe-dev mailing list<br>
<a href="mailto:cfe-dev@cs.uiuc.edu">cfe-dev@cs.uiuc.edu</a><br>
<a href="http://lists.cs.uiuc.edu/mailman/listinfo/cfe-dev" target="_blank">http://lists.cs.uiuc.edu/mailman/listinfo/cfe-dev</a><br>
</div></div></blockquote></div><br></div></div>