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

            Bug ID: 110102
           Summary: [13 regression] initializer_list ctors of containers
                    skip Allocator_traits::construct, copies move-only
                    type
           Product: gcc
           Version: 13.1.0
            Status: UNCONFIRMED
          Keywords: accepts-invalid
          Severity: normal
          Priority: P3
         Component: libstdc++
          Assignee: unassigned at gcc dot gnu.org
          Reporter: arthur.j.odwyer at gmail dot com
  Target Milestone: ---

// https://godbolt.org/z/4Gq3TWE6M
#include <list>
struct A {
    A(int) {}
    A(const A&) = delete;
    A(A&&) {}
};
int main() {
    std::list<A> v = {1,2,3};
}

This should be ill-formed, but GCC 13.1 accepts it!
GCC 12.3 and earlier correctly reject it.

This is supposed to be constructing a std::initializer_list<A> and calling
`list::list(initializer_list<A>)`, which should then complain because `A(const
A&)` is deleted.

My guess as to what's happening here:
- We're definitely calling list(initializer_list<A>)
- It's calling _M_range_initialize(il.begin(), il.end())
- That's calling __uninitialized_copy_a
- That's probably somehow deciding that because `A` is trivially copyable, we
can just memcpy it. I.e. bug #89164 redux.

Even if it were copyable, we still wouldn't be allowed to bypass
`allocator_traits::construct`. The above snippet uses std::allocator, but I
originally found a more complicated case with pmr::polymorphic_allocator:

// https://godbolt.org/z/ToT6dW5dM
#include <cstdio>
#include <memory_resource>
#include <vector>
struct Widget {
    using allocator_type = std::pmr::polymorphic_allocator<Widget>;
    Widget(int i) : i_(i) {}
    explicit Widget(int i, allocator_type) : i_(i) {}
    explicit Widget(const Widget& rhs, allocator_type) : i_(rhs.i_ + 100) {}
    int i_;
};
static_assert(std::is_trivially_copyable_v<Widget>);
int main() {
    std::pmr::vector<Widget> v = {1,2,3};
    printf("%d %d %d\n", v[0].i_, v[1].i_, v[2].i_);
}

My understanding is that this should print "101 102 103", as GCC 12 does. But
GCC 13.1 prints "1 2 3" instead.

Reply via email to