https://gcc.gnu.org/bugzilla/show_bug.cgi?id=109305

Jonathan Wakely <redi at gcc dot gnu.org> changed:

           What    |Removed                     |Added
----------------------------------------------------------------------------
             Status|UNCONFIRMED                 |WAITING
     Ever confirmed|0                           |1
   Last reconfirmed|                            |2023-03-27

--- Comment #3 from Jonathan Wakely <redi at gcc dot gnu.org> ---
(In reply to Marc-André Laverdière from comment #0)
> Created attachment 54773 [details]
> Self-contained preprocessed reproducer
> 
> In basic_string& operator=(const basic_string& __str), I noticed this line:
> 
> auto __alloc = __str._M_get_allocator();
> 
> This creates a local copy of the allocator object. I doubt this is
> intentional, but I could be wrong.

It's completely intentional. The function needs to allocate new storage, so it
needs a non-const allocator that compares equal to __str.get_allocator().

As the comment hints at, the order of the operations is important. We don't
want to update the allocator in *this until after we've allocated the storage,
because that could throw. So we make a copy of the allocator, use it to
allocate memory, then only if that was successful we replace the allocator in
*this.

Why do you think this local copy is a problem?

Reply via email to