From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: by sourceware.org (Postfix, from userid 48) id 4B3CA3858C52; Thu, 2 Feb 2023 21:00:52 +0000 (GMT) DKIM-Filter: OpenDKIM Filter v2.11.0 sourceware.org 4B3CA3858C52 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gcc.gnu.org; s=default; t=1675371652; bh=+83StoBC4TG6a6Xs/ubSUZ7u7fmxBwJdBFW9F3m7EbY=; h=From:To:Subject:Date:From; b=DMdtFgTgxeUhrdfqo8lnWTBXGFC4yKAp7s+6rX7p6eJfo8lU+mU1DN4qQsCEMLDAk kNIWNZi1LlKGixGg/f8MTnfGaQmvcDekE3DM/gEeS67LMAh2XZMQhgKBAyL3wd26QH HzfBZFQ1GiBt0YPOElChuNY9wgXYN8xmZGsJOwHc= From: "eteran at alum dot rit.edu" To: gcc-bugs@gcc.gnu.org Subject: [Bug libstdc++/108645] New: Change in behavior, std::accumulate doesn't always work as expected in C++20 builds Date: Thu, 02 Feb 2023 21:00:51 +0000 X-Bugzilla-Reason: CC X-Bugzilla-Type: new X-Bugzilla-Watch-Reason: None X-Bugzilla-Product: gcc X-Bugzilla-Component: libstdc++ X-Bugzilla-Version: unknown X-Bugzilla-Keywords: X-Bugzilla-Severity: normal X-Bugzilla-Who: eteran at alum dot rit.edu X-Bugzilla-Status: UNCONFIRMED X-Bugzilla-Resolution: X-Bugzilla-Priority: P3 X-Bugzilla-Assigned-To: unassigned at gcc dot gnu.org X-Bugzilla-Target-Milestone: --- X-Bugzilla-Flags: X-Bugzilla-Changed-Fields: bug_id short_desc product version bug_status bug_severity priority component assigned_to reporter target_milestone Message-ID: Content-Type: text/plain; charset="UTF-8" Content-Transfer-Encoding: quoted-printable X-Bugzilla-URL: http://gcc.gnu.org/bugzilla/ Auto-Submitted: auto-generated MIME-Version: 1.0 List-Id: https://gcc.gnu.org/bugzilla/show_bug.cgi?id=3D108645 Bug ID: 108645 Summary: Change in behavior, std::accumulate doesn't always work as expected in C++20 builds Product: gcc Version: unknown Status: UNCONFIRMED Severity: normal Priority: P3 Component: libstdc++ Assignee: unassigned at gcc dot gnu.org Reporter: eteran at alum dot rit.edu Target Milestone: --- I encountered an interesting change in behavior today involving playing with std::move_iterator types using the following code: ``` #include #include #include #include #include void print_v(const char *rem, const std::vector &v) { std::cout << rem; for (const std::string &s : v) std::cout << '"' << s << '"' << ' '; std::cout << '\n'; } int main() { std::vector v =3D {"this", "_", "is", "_", "an", "_", "example"}; print_v("Old contents of the vector: ", v); std::string concat =3D std::accumulate(std::make_move_iterator(v.begin()), =20=20=20=20=20=20=20=20=20=20=20=20=20=20=20=20=20=20=20=20=20=20=20=20=20= =20=20=20=20=20=20=20=20=20=20=20=20=20=20=20=20=20=20=20=20=20=20=20=20=20= =20=20=20=20=20=20=20=20=20=20=20=20=20=20=20=20=20=20=20=20=20=20=20=20=20= =20=20=20=20 std::make_move_iterator(v.end()), =20=20=20=20=20=20=20=20=20=20=20=20=20=20=20=20=20=20=20=20=20=20=20=20=20= =20=20=20=20=20=20=20=20=20=20=20=20=20=20=20=20=20=20=20=20=20=20=20=20=20= =20=20=20=20=20=20=20=20=20=20=20=20=20=20=20=20=20=20=20=20=20=20=20=20=20= =20=20=20=20 std::string()); print_v("New contents of the vector: ", v); std::cout << "Concatenated as string: " << '"' << concat << '"' << '\n'; } ``` The expected output is: ``` Old contents of the vector: "this" "_" "is" "_" "an" "_" "example"=20 New contents of the vector: "" "" "" "" "" "" ""=20 Concatenated as string: "this_is_an_example" ``` And that is the output I get when compiling with `-std=3Dc++17`. So the expectation is that the moved from strings become empty. However, when using C++20, where `std::accumulate` is now defined to use `std::move`, the output is this instead: ``` Old contents of the vector: "this" "_" "is" "_" "an" "_" "example"=20 New contents of the vector: "this" "_" "is" "_" "an" "_" "example"=20 Concatenated as string: "this_is_an_example" ``` A few thoughts: 1. I'm not sure this is a "bug" since moved from objects are in an unspecif= ied, valid state. Being "unchanged" seems to fit that description just fine. 2. I'm guessing that this is a weird situation where the `operator+` overlo= ad that has a `basic_string &&` and the lhs isn't stealing the guts as expected for some reason. Tested with both clang and gcc trunk, so it seems to be a library level iss= ue. C++20 Example: https://godbolt.org/z/7MMqWbxEr C++17 Example: https://godbolt.org/z/6YnrrE5zf=