public inbox for gcc-bugs@sourceware.org
help / color / mirror / Atom feed
* [Bug c++/113020] New: Explicit template instantiation of template specialization using a template base class fails after extern template declaration with linker error
@ 2023-12-14 13:47 adrian at woeltche dot de
  2023-12-14 17:11 ` [Bug c++/113020] " pinskia at gcc dot gnu.org
                   ` (2 more replies)
  0 siblings, 3 replies; 4+ messages in thread
From: adrian at woeltche dot de @ 2023-12-14 13:47 UTC (permalink / raw)
  To: gcc-bugs

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.

^ permalink raw reply	[flat|nested] 4+ messages in thread

* [Bug c++/113020] Explicit template instantiation of template specialization using a template base class fails after extern template declaration with linker error
  2023-12-14 13:47 [Bug c++/113020] New: Explicit template instantiation of template specialization using a template base class fails after extern template declaration with linker error adrian at woeltche dot de
@ 2023-12-14 17:11 ` pinskia at gcc dot gnu.org
  2023-12-15  5:44 ` pinskia at gcc dot gnu.org
  2023-12-15  5:47 ` pinskia at gcc dot gnu.org
  2 siblings, 0 replies; 4+ messages in thread
From: pinskia at gcc dot gnu.org @ 2023-12-14 17:11 UTC (permalink / raw)
  To: gcc-bugs

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

--- Comment #1 from Andrew Pinski <pinskia at gcc dot gnu.org> ---
Side note, I wish "Compiler Explorer" was able to a simple make file instead of
cmake.

^ permalink raw reply	[flat|nested] 4+ messages in thread

* [Bug c++/113020] Explicit template instantiation of template specialization using a template base class fails after extern template declaration with linker error
  2023-12-14 13:47 [Bug c++/113020] New: Explicit template instantiation of template specialization using a template base class fails after extern template declaration with linker error adrian at woeltche dot de
  2023-12-14 17:11 ` [Bug c++/113020] " pinskia at gcc dot gnu.org
@ 2023-12-15  5:44 ` pinskia at gcc dot gnu.org
  2023-12-15  5:47 ` pinskia at gcc dot gnu.org
  2 siblings, 0 replies; 4+ messages in thread
From: pinskia at gcc dot gnu.org @ 2023-12-15  5:44 UTC (permalink / raw)
  To: gcc-bugs

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

--- Comment #2 from Andrew Pinski <pinskia at gcc dot gnu.org> ---
The commands needed to get the link error without using cmake:
```
g++ -std=c++20   -c -o main.o main.cpp
g++ -std=c++20   -c -o base.o base.cpp
g++ -std=c++20   -c -o sub.o sub.cpp
g++ -o app main.o base.o sub.o
```

^ permalink raw reply	[flat|nested] 4+ messages in thread

* [Bug c++/113020] Explicit template instantiation of template specialization using a template base class fails after extern template declaration with linker error
  2023-12-14 13:47 [Bug c++/113020] New: Explicit template instantiation of template specialization using a template base class fails after extern template declaration with linker error adrian at woeltche dot de
  2023-12-14 17:11 ` [Bug c++/113020] " pinskia at gcc dot gnu.org
  2023-12-15  5:44 ` pinskia at gcc dot gnu.org
@ 2023-12-15  5:47 ` pinskia at gcc dot gnu.org
  2 siblings, 0 replies; 4+ messages in thread
From: pinskia at gcc dot gnu.org @ 2023-12-15  5:47 UTC (permalink / raw)
  To: gcc-bugs

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

Andrew Pinski <pinskia at gcc dot gnu.org> changed:

           What    |Removed                     |Added
----------------------------------------------------------------------------
             Status|UNCONFIRMED                 |RESOLVED
         Resolution|---                         |DUPLICATE

--- Comment #3 from Andrew Pinski <pinskia at gcc dot gnu.org> ---
So yes it is exactly the same as PR 104634 .

*** This bug has been marked as a duplicate of bug 104634 ***

^ permalink raw reply	[flat|nested] 4+ messages in thread

end of thread, other threads:[~2023-12-15  5:47 UTC | newest]

Thread overview: 4+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2023-12-14 13:47 [Bug c++/113020] New: Explicit template instantiation of template specialization using a template base class fails after extern template declaration with linker error adrian at woeltche dot de
2023-12-14 17:11 ` [Bug c++/113020] " pinskia at gcc dot gnu.org
2023-12-15  5:44 ` pinskia at gcc dot gnu.org
2023-12-15  5:47 ` pinskia at gcc dot gnu.org

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).