public inbox for gcc-bugs@sourceware.org
help / color / mirror / Atom feed
* [Bug libstdc++/98473] New: std::vector<T>::insert(pos, first, last) doesn't compile for T which has a deleted assignment operator
@ 2020-12-29 18:05 b.stanimirov at abv dot bg
  2020-12-31 13:36 ` [Bug libstdc++/98473] " redi at gcc dot gnu.org
                   ` (3 more replies)
  0 siblings, 4 replies; 5+ messages in thread
From: b.stanimirov at abv dot bg @ 2020-12-29 18:05 UTC (permalink / raw)
  To: gcc-bugs

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

            Bug ID: 98473
           Summary: std::vector<T>::insert(pos, first, last) doesn't
                    compile for T which has a deleted assignment operator
           Product: gcc
           Version: unknown
            Status: UNCONFIRMED
          Severity: normal
          Priority: P3
         Component: libstdc++
          Assignee: unassigned at gcc dot gnu.org
          Reporter: b.stanimirov at abv dot bg
  Target Milestone: ---

Create a class `X` which is copy-constructible but not copy-assignable

```
struct X {
    X();
    X(const X&);
    X& operator=(const X&) = delete; // !!
    X(X&&) noexcept;
    X& operator=(X&&) noexcept;
    int data = 54;
};
```

Have vectors of X `a` and `b`. Try to add all elements of b at the front of a:

```
void add_to_front(std::vector<X>& a, const std::vector<X>& b) {
    a.insert(a.begin(), b.begin(), b.end());
}
```

Live demo: https://godbolt.org/z/K1WT8n

It doesn't compile. My guess is that code which will never get invoked, still
needs to compile (or, worse yet, copy assignment does get invoked?!)

The only way to achieve the desired behavior efficiently is to use a custom
vector implementation. There is a stackoverflow question which has some
workarounds *with worse performance*:
https://stackoverflow.com/questions/65489039/how-to-efficiently-insert-multiple-copy-constructible-but-not-copy-assignable-el

This does compile and work on msvc, so a compile-time check for the
copy-assignment code is possible.

As pointed out in the stackoverflow thread, copy-assignability is not listed
here: https://en.cppreference.com/w/cpp/container/vector/insert#Parameters

This looks like a bug in libstdc++ (as opposed to a omission by the standard)

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

end of thread, other threads:[~2021-01-08 15:24 UTC | newest]

Thread overview: 5+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2020-12-29 18:05 [Bug libstdc++/98473] New: std::vector<T>::insert(pos, first, last) doesn't compile for T which has a deleted assignment operator b.stanimirov at abv dot bg
2020-12-31 13:36 ` [Bug libstdc++/98473] " redi at gcc dot gnu.org
2021-01-01 16:35 ` b.stanimirov at abv dot bg
2021-01-05  7:08 ` b.stanimirov at abv dot bg
2021-01-08 15:24 ` 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).