[cfe-dev] libc++ std::map self-assignment bug

Kal b17c0de at gmail.com
Thu Nov 28 23:50:03 PST 2013


Am 29.11.13 04:17, schrieb Howard Hinnant:
> On Nov 28, 2013, at 9:58 PM, Karen Shaeffer <shaeffer at neuralscape.com> wrote:
>
>> On Fri, Nov 29, 2013 at 02:19:44AM +0100, Kal wrote:
>>> Hi Howard, etc,
>>>
>>> libc++3.4 (rc1/trunk) std::map doesn't support self-assignment for std <
>>> c++11. The relevant code is:
>>>
>>>    _LIBCPP_INLINE_VISIBILITY
>>>    map& operator=(const map& __m)
>>>        {
>>> #if __cplusplus >= 201103L
>>>            __tree_ = __m.__tree_;
>>> #else
>>>            __tree_.clear();
>>>            __tree_.value_comp() = __m.__tree_.value_comp();
>>>            __tree_.__copy_assign_alloc(__m.__tree_);
>>>            insert(__m.begin(), __m.end());
>>> #endif
>>>            return *this;
>>>        }
>>>
>>> Maybe should be like:
>>>
>>>    _LIBCPP_INLINE_VISIBILITY
>>>    map& operator=(const map& __m)
>>>        {
>>> #if __cplusplus >= 201103L
>>>            __tree_ = __m.__tree_;
>>> #else
>>>            if (this != &__m) {
>>>                __tree_.clear();
>>>                __tree_.value_comp() = __m.__tree_.value_comp();
>>>                __tree_.__copy_assign_alloc(__m.__tree_);
>>>                insert(__m.begin(), __m.end());
>>>            }
>>> #endif
>>>            return *this;
>>>        }
>>>
>>> I see the same issue with unordered_map& operator=(const unordered_map&
>>> __u).
>>>
>>> Thanks!
>>> Kal
>> --- end quoted text ---
>>
>> Hi Kal,
>> I don't speak for Howard or anyone else. But my understanding is the stl library
>> generally doesn't perform checks like that with the goal of best possible performance.
>> I see a lot of code in stdlibc++ just like that.
> 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.
>
> Howard
>
Hi Howard,
Thanks for the reply. Do you think a fix for this will be committed for
the 3.4 release?

Also could you explain why the special check for __cplusplus < 201103L
is necessary at all. Both branches are doing almost the same thing
(looking into the implementation of __tree). Is there some subtle
difference in the standard that requires a different implementation here?

Kal



More information about the cfe-dev mailing list