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

            Bug ID: 125280
           Summary: [reflection] -freflection flag causes incomplete type
                    errors on class members that reference their owning
                    type within a template class parameter
           Product: gcc
           Version: 16.1.1
            Status: UNCONFIRMED
          Severity: normal
          Priority: P3
         Component: c++
          Assignee: unassigned at gcc dot gnu.org
          Reporter: lwaltonne at gmail dot com
  Target Milestone: ---

Created attachment 64438
  --> https://gcc.gnu.org/bugzilla/attachment.cgi?id=64438&action=edit
Pre-processed .ii file of offending code

When declaring a class member, if their type is a template class that
references the class member's owning type, compilation will fail with an
incomplete type error.

GCC version: gcc version 16.1.1 20260430
OS: x86_64, Arch Linux 7.0.3

Bug initially identified by Jorrit Rouwe, https://github.com/jrouwe
Jorrit also wrote this minimal reproduction in Godbolt:
https://godbolt.org/z/fvdEMcceo

Additionally, I've replicated the above reproduction locally and have attached
the .ii file generated from the -save-temps flag

Compilation command: gcc -c main.cpp -o main.o -std=c++26 -freflection
-freport-bug -save-temps

Code (main.cpp):
```
template <class T, int N> class Array {
public:
  T mElements[N];
};

class Element {
public:
  static const Array<Element, 10> mData;
};

const Array<Element, 10> Element::mData;
```

Compiler output:
```
main.cpp: In instantiation of ‘class Array<Element, 10>’:
main.cpp:8:35:   required from here
    8 |   static const Array<Element, 10> mData;
      |                                   ^~~~~
main.cpp:3:5: error: ‘Array<T, N>::mElements’ has incomplete type
    3 |   T mElements[N];
      |     ^~~~~~~~~
main.cpp:6:7: note: forward declaration of ‘class Element’
    6 | class Element {
      |       ^~~~~~~
main.cpp:11:26: error: uninitialized ‘const Element::mData’ [-fpermissive]
   11 | const Array<Element, 10> Element::mData;
      |                          ^~~~~~~
main.cpp:1:33: note: ‘const class Array<Element, 10>’ has no user-provided
default constructor
    1 | template <class T, int N> class Array {
      |                                 ^~~~~
main.cpp:3:5: note: and the implicitly-defined constructor does not initialize
‘<typeprefixerror>Array<Element, 10>::mElements’
    3 |   T mElements[N];
      |     ^~~~~~~~~
```

Reply via email to