public inbox for gcc-bugs@sourceware.org
help / color / mirror / Atom feed
* [Bug libstdc++/108645] New: Change in behavior, std::accumulate doesn't always work as expected in C++20 builds
@ 2023-02-02 21:00 eteran at alum dot rit.edu
  2023-02-02 21:05 ` [Bug libstdc++/108645] " eteran at alum dot rit.edu
                   ` (4 more replies)
  0 siblings, 5 replies; 6+ messages in thread
From: eteran at alum dot rit.edu @ 2023-02-02 21:00 UTC (permalink / raw)
  To: gcc-bugs

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

            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 <vector>
#include <iostream>
#include <string>
#include <numeric>
#include <iterator>

void print_v(const char *rem, const std::vector<std::string> &v) {
        std::cout << rem;
        for (const std::string &s : v)
                std::cout << '"' << s << '"' << ' ';
        std::cout << '\n';
}

int main() {

        std::vector<std::string> v = {"this", "_", "is", "_", "an", "_",
"example"};

        print_v("Old contents of the vector: ", v);

        std::string concat =
std::accumulate(std::make_move_iterator(v.begin()),
                                                                               
 std::make_move_iterator(v.end()),
                                                                               
 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" 
New contents of the vector: "" "" "" "" "" "" "" 
Concatenated as string: "this_is_an_example"
```

And that is the output I get when compiling with `-std=c++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" 
New contents of the vector: "this" "_" "is" "_" "an" "_" "example" 
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 unspecified,
valid state. Being "unchanged" seems to fit that description just fine.

2. I'm guessing that this is a weird situation where the `operator+` overload
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 issue.

C++20 Example: https://godbolt.org/z/7MMqWbxEr
C++17 Example: https://godbolt.org/z/6YnrrE5zf

^ permalink raw reply	[flat|nested] 6+ messages in thread

end of thread, other threads:[~2023-02-03 10:08 UTC | newest]

Thread overview: 6+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2023-02-02 21:00 [Bug libstdc++/108645] New: Change in behavior, std::accumulate doesn't always work as expected in C++20 builds 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
2023-02-03 10:01 ` redi at gcc dot gnu.org
2023-02-03 10:08 ` redi at gcc dot gnu.org

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).