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

--- Comment #12 from Jonathan Wakely <redi at gcc dot gnu.org> ---
That would also benefit this overload:

      basic_string&
      assign(initializer_list<_CharT> __l)
      { return this->assign(__l.begin(), __l.size()); }

That currently goes via replace(begin(), end(), l.begin(), l.end()) but we know
that an initializer_list cannot possibly alias the string's contents.

But we can do even better and avoid any copy if __l.size() <= capacity():

      basic_string&
      assign(initializer_list<_CharT> __l)
      {
        const size_type __n = __l.size();
        if (__n > capacity())
          *this = basic_string(__l.begin(), __l.end(), get_allocator());
        else
          {
            if (__n)
              _S_copy(_M_data(), __l.begin(), __n);
            _M_set_length(__n);
          }
        return *this;
      }

Reply via email to