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

            Bug ID: 122421
           Summary: Instantiation in a module does not affect a static
                    array defined outside the module.
           Product: gcc
           Version: 16.0
            Status: UNCONFIRMED
          Severity: normal
          Priority: P3
         Component: c++
          Assignee: unassigned at gcc dot gnu.org
          Reporter: d7d1cd at mail dot ru
  Target Milestone: ---

If a class template is instantiated within a module, a static array defined
outside of this class is not included in the object file, and a link error
occurs (example here: https://godbolt.org/z/oGTdMWhK9)

$ cat module.ixx
export module M;
export template <typename> struct Type {
static const int arr[3];
};

template <typename _> const int Type<_>::arr[] = {42, 43, 44};

export struct A : Type<int> {};

$ cat main.cc
#include <iostream>
import M;

int main() {
const auto a = Type<int>::arr[0];
std::cout << a << std::endl;

auto b = A::arr[0];
}

linker output:
linux-gnu/bin/ld: CMakeFiles/app.dir/main.cc.o: in function `main':
main.cc:(.text+0xa): undefined reference to `Type@M<int>::arr'

However, if the module does not instantiate the template with a specific type,
then the error does not occur: https://godbolt.org/z/ThrPdf1hs

Reply via email to