[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
gcc-bugzilla@gcc.gnu.org
Tue Dec 29 18:05:22 GMT 2020
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)
More information about the Gcc-bugs
mailing list