Rocksolid Light

Welcome to novaBBS (click a section below)

mail  files  register  newsreader  groups  login


Stellar rays prove fibbing never pays. Embezzlement is another matter.

devel / comp.lang.c++ / Explicit instantiation: why a weak symbol?

o Explicit instantiation: why a weak symbol?Andrey Tarasevich

Explicit instantiation: why a weak symbol?


  copy mid

  copy link   Newsgroups: comp.lang.c++
From: (Andrey Tarasevich)
Newsgroups: comp.lang.c++
Subject: Explicit instantiation: why a weak symbol?
Date: Thu, 24 Aug 2023 23:10:33 -0700
Organization: A noiseless patient Spider
Lines: 49
Message-ID: <uc9gkr$3t5vu$>
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8; format=flowed
Content-Transfer-Encoding: 7bit
Injection-Date: Fri, 25 Aug 2023 06:10:35 -0000 (UTC)
Injection-Info:; posting-host="d1abbe6b301afbdecbb61dcc5dd31b91";
logging-data="4102142"; mail-complaints-to=""; posting-account="U2FsdGVkX1+IwSAmNz07pg6LpLmLQa+l"
User-Agent: Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:102.0) Gecko/20100101
Cancel-Lock: sha1:c3DYP2ExqJ6j33Vp3t1eYr05ym4=
Content-Language: en-US
 by: Andrey Tarasevich - Fri, 25 Aug 2023 06:10 UTC

Consider the following translation unit:

template <typename T> void foo(T t) {}

template <> void foo<int>(int i) {}
// Explicit specialization

template void foo<double>(double d);
// Explicit instantiation

When compiling this unit with GCC or Clang, the compiler generates an
object file with bodies for `foo<int>` and `foo<double>`, all as
expected. However, explicit specialization results in a "strong" symbol
for `foo<int>`, while explicit instantiation for `foo<double>` produces
a weak symbol.


A "strong" symbol for `foo<int>` makes perfect sense. But why a weak
symbol for `foo<double>`? Why not a "strong" one as well?

For example, a program that includes two copies of translation unit like

template <typename T> void foo(T t) {}
template void foo<double>(double d);

is formally invalid, since it is illegal to have two explicit
instantiation definitions for the same set of template parameters (see Yet, nether GCC nor Clang
is able to detect this error, since they generate weak symbols for
`foo<double>` in both translation units.

The standard also does not require diagnostic for this error, even
though to my naive eye it seems to be as easy to diagnose as any
ordinary "multiple definition" error. Just make the explicit
instantiation a "strong" symbol and the linker will catch the error.

But apparently there's a reason the standard require no diagnostic, and
there's a reason GCC and Clang make it a weak symbol. Anyone has any
idea what that reason might be?

All I could find on the Net is this: But this seems to be
too niche.

Best regards,

devel / comp.lang.c++ / Explicit instantiation: why a weak symbol?


rocksolid light 0.9.81
clearnet tor