public inbox for gcc-bugs@sourceware.org
help / color / mirror / Atom feed
From: "redi at gcc dot gnu.org" <gcc-bugzilla@gcc.gnu.org>
To: gcc-bugs@gcc.gnu.org
Subject: [Bug libstdc++/108645] Change in behavior, std::accumulate doesn't always work as expected in C++20 builds
Date: Fri, 03 Feb 2023 09:32:49 +0000	[thread overview]
Message-ID: <bug-108645-4-ZPgBrDxhm3@http.gcc.gnu.org/bugzilla/> (raw)
In-Reply-To: <bug-108645-4@http.gcc.gnu.org/bugzilla/>

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

--- Comment #3 from Jonathan Wakely <redi at gcc dot gnu.org> ---
(In reply to Evan Teran from comment #1)
> Which results in the same behavior, so it appears to be that the:
> 
> ```
> basic_string operator+(basic_string &&, basic_string &&)
> ```
> 
> Overload doesn't steal the guts of the rhs at all?

It does if:
- the allocators are equal,
- the LHS does not have sufficient capacity for the result, and
- the RHS does have sufficient capacity for the result.

In your example, all your strings fit in the SSO buffer inside the std::string
object, so the LHS has sufficient capacity for the result. And there's nothing
to steal from the RHS anyway, as it doesn't own any allocated memory.

The observed behaviour is not a bug, because as you say, leaving the RHS
non-empty is a valid state. The implementation is pretty clear:

    {
#if _GLIBCXX_USE_CXX11_ABI
      using _Alloc_traits = allocator_traits<_Alloc>;
      bool __use_rhs = false;
      if _GLIBCXX17_CONSTEXPR (typename _Alloc_traits::is_always_equal{})
        __use_rhs = true;
      else if (__lhs.get_allocator() == __rhs.get_allocator())
        __use_rhs = true;
      if (__use_rhs)
#endif
        {
          const auto __size = __lhs.size() + __rhs.size();
          if (__size > __lhs.capacity() && __size <= __rhs.capacity())
            return std::move(__rhs.insert(0, __lhs));
        }
      return std::move(__lhs.append(__rhs));
    }

If the RHS object is used (because all the conditions above are true) then it
will be moved into the return value and so left empty.

If the LHS is used then the RHS is unchanged.

  parent reply	other threads:[~2023-02-03  9:32 UTC|newest]

Thread overview: 6+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2023-02-02 21:00 [Bug libstdc++/108645] New: " eteran at alum dot rit.edu
2023-02-02 21:05 ` [Bug libstdc++/108645] " eteran at alum dot rit.edu
2023-02-03  1:40 ` pinskia at gcc dot gnu.org
2023-02-03  9:32 ` redi at gcc dot gnu.org [this message]
2023-02-03 10:01 ` redi at gcc dot gnu.org
2023-02-03 10:08 ` redi at gcc dot gnu.org

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=bug-108645-4-ZPgBrDxhm3@http.gcc.gnu.org/bugzilla/ \
    --to=gcc-bugzilla@gcc.gnu.org \
    --cc=gcc-bugs@gcc.gnu.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).