https://gcc.gnu.org/bugzilla/show_bug.cgi?id=116746
Bug ID: 116746 Summary: Explicit specializations of static variable templates are incorrectly given external linkage Product: gcc Version: 15.0 Status: UNCONFIRMED Severity: normal Priority: P3 Component: c++ Assignee: unassigned at gcc dot gnu.org Reporter: tom at honermann dot net Target Milestone: --- As demonstrated by the following test case, GCC emits symbols with external linkage for explicit specializations of variable templates declared with the `static` storage class specifier. Such explicit specializations should be given the same linkage as the primary template; internal linkage for this example. This expectation is supported by the following sections of the C++ standard: Per [temp.expl.spec]p4: > An explicit specialization does not introduce a name ([basic.scope.scope]). … Per [basic.link]p2: > A name can have external linkage, module linkage, internal linkage, or no > linkage, as determined by the rules below. The test case consists of two source files, each of which declares a static variable template named `ptr` and an associated explicit specialization of `ptr<int>`. When compiled and linked, link fails due to multiple definitions of `ptr<int>` (thus demonstrating that they were given external linkage). Commenting out one or both of the explicit specializations suffices for the program to link successfully and to run without assertion failures thus demonstrating that the primary templates (and the specializations implicitly instantiated from them) were given internal linkage. Makefile: ``` all: main clean: rm -f main.o t.o main main.o: main.cpp g++ -c main.cpp t.o: t.cpp g++ -c t.cpp main: main.o t.o g++ main.o t.o -o main ``` main.cpp: ``` #include <cassert> static int x; template <typename T> static void* ptr = &x; template <> void* ptr<int> = &x; extern void* f(); extern void* g(); int main() { assert(ptr<char> != f()); assert(ptr<int> != g()); } ``` t.cpp: ``` static int x; template <typename T> static void* ptr = &x; template <> void* ptr<int> = &x; void* f() { return ptr<char>; } void* g() { return ptr<int>; } ``` An example build and invocation follows: ``` $ make g++ -c main.cpp g++ -c t.cpp g++ main.o t.o -o main ld: t.o:(.data+0x0): multiple definition of `ptr<int>'; main.o:(.data+0x0): first defined here collect2: error: ld returned 1 exit status make: *** [Makefile:9: main] Error 1 ``` This appears to be a common compiler bug as both Clang and MSVC exhibit similar behavior as can be observed at https://godbolt.org/z/fzsdTG9rM. (Unfortunately, that link won't demonstrate MSVC behavior currently due to a timeout issue tracked at https://github.com/compiler-explorer/compiler-explorer/issues/6742). A Clang bug report can be found at https://github.com/llvm/llvm-project/issues/35211 and a MSVC bug report is available at https://developercommunity.visualstudio.com/t/Explicit-specializations-of-static-varia/10746502. Note that the C++ standard does not permit an explicit specialization to be declared with the static storage class specifier and it is therefore not possible to explicitly declare that the explicit specializations have internal linkage. Per [temp.expl.spec]p2: > The declaration in an explicit-specialization shall not be an > export-declaration. An explicit specialization shall not use a > storage-class-specifier ([dcl.stc]) other than thread_local.