https://gcc.gnu.org/bugzilla/show_bug.cgi?id=113020
Bug ID: 113020 Summary: Explicit template instantiation of template specialization using a template base class fails after extern template declaration with linker error Product: gcc Version: 13.2.0 Status: UNCONFIRMED Severity: normal Priority: P3 Component: c++ Assignee: unassigned at gcc dot gnu.org Reporter: adrian at woeltche dot de Target Milestone: --- Created attachment 56878 --> https://gcc.gnu.org/bugzilla/attachment.cgi?id=56878&action=edit Compiler Explorer code dump Dear GCC team, I encountered a bug in GCC 13.2, initially with using the official Docker GCC container "gcc:13" (which is GCC 13.2 at the moment). When I define a template base class with a template specialization and use extern template declaration for both the regular template class as well as the template specialization, with explicit template instantiation in a separate cpp file, the code gets compiled and is successfully linked. When I inherit a template sub class from the template base class, extern template declare and explicit template instantiate, this also works. When I make a specialization of the sub class that inherits from the base class and extern template declare it, the explicit template instantiation code is not generated in the object file and I receive a linker error. However, with Clang 17.0 the code compiles, links, and executes without an error. The example may look a bit complex, but it is similarly constructed as my original code. Please see the following Compiler Explorer example: https://gcc.godbolt.org/z/GvoTxKzKd It shows the linker error message for GCC 13.2 as well as that the same code is working with Clang 17.0. It looks like all older versions of GCC down to 10.1 emit the same error. Clang works with all versions down to 11.0.0. When you uncomment line 41 in sub.hpp the code compiles, since the implicit template instantiation works. It does not matter for GCC whether line 6 in sub.cpp exists or not, but when the line 6 in sub.cpp is commented, Clang 17.0 fails, too, as expected, since the explicit template instantiation is then missing. The expected behavior is that the explicit template instantiation in line 6 of sub.cpp works similarly as in Clang 17.0 and generated the code, so that the linking works. In my initial case on my dev system, I examined the object file with nm and saw that the code is missing in GCC and exists with Clang. I am not sure if this is similar to Bug #104634. The code is a little bit different since in this case, the problem arises with a subclass and not with another specialization of the same class. Thank you for your support! PS: The attachment contains the same code as the Compiler Explorer link.