[llvm-bugs] [Bug 48619] New: Allow std::vector::insert to compile when elements have a deleted assignment operator

via llvm-bugs llvm-bugs at lists.llvm.org
Tue Dec 29 00:40:14 PST 2020


https://bugs.llvm.org/show_bug.cgi?id=48619

            Bug ID: 48619
           Summary: Allow std::vector::insert to compile when elements
                    have a deleted assignment operator
           Product: libc++
           Version: unspecified
          Hardware: All
                OS: All
            Status: NEW
          Severity: normal
          Priority: P
         Component: All Bugs
          Assignee: unassignedclangbugs at nondot.org
          Reporter: bstanimirov6 at gmail.com
                CC: llvm-bugs at lists.llvm.org, mclow.lists at gmail.com

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 *worse*
workarounds:
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.

-- 
You are receiving this mail because:
You are on the CC list for the bug.
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.llvm.org/pipermail/llvm-bugs/attachments/20201229/10d78bde/attachment.html>


More information about the llvm-bugs mailing list