[Bug c++/82873] New: Generated copy constructor calls constructors for 0-sized array members

matthew.fernandez at gmail dot com gcc-bugzilla@gcc.gnu.org
Tue Nov 7 02:41:00 GMT 2017


            Bug ID: 82873
           Summary: Generated copy constructor calls constructors for
                    0-sized array members
           Product: gcc
           Version: 7.2.0
            Status: UNCONFIRMED
          Severity: normal
          Priority: P3
         Component: c++
          Assignee: unassigned at gcc dot gnu.org
          Reporter: matthew.fernandez at gmail dot com
  Target Milestone: ---

Hello there, I'm trying to do something a little unorthodox and ran into an
undefined reference during linking. It appears that a generated copy
constructor calls another copy constructor that itself is not generated. A
minimised example is:

    #include <vector>

    class Foo {
      std::vector<int> x[0];

    int main(void) {
      Foo a;
      Foo b(a);
      return 0;

Compiling this yields:

    $ g++ -std=c++17 -W -Wall -Wextra main.cc
    /tmp/ccfexx8L.o: In function `Foo::Foo(Foo const&)':
    main.cc:(.text._ZN3FooC2ERKS_[_ZN3FooC5ERKS_]+0x35): undefined reference to
`std::vector<int, std::allocator<int> >::vector(std::vector<int,
std::allocator<int> > const&)'

It seems this is related to the 0-sized array member, which I realise is
non-standard (https://gcc.gnu.org/onlinedocs/gcc/Zero-Length.html). Making the
array member size 1 results in the relevant vector copy constructor being
generated, as does inserting the following otherwise-useless code into the
translation unit:

    static inline std::vector<int> dummy(const std::vector<int> &p) {
      return p;

This behaviour surprised me because I expected GCC to either (1) emit a compile
error or (2) generate the relevant constructors. Is this known/expected

Possibly of relevance PR 42121 and PR 14799.

More information about the Gcc-bugs mailing list