Rocksolid Light

Welcome to RetroBBS

mail  files  register  newsreader  groups  login

Message-ID:  

For every problem there is one solution which is simple, neat, and wrong. -- H. L. Mencken


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

SubjectAuthor
o Explicit instantiation: why a weak symbol?Andrey Tarasevich

1
Explicit instantiation: why a weak symbol?

<uc9gkr$3t5vu$1@dont-email.me>

  copy mid

https://www.rocksolidbbs.com/devel/article-flat.php?id=1183&group=comp.lang.c%2B%2B#1183

  copy link   Newsgroups: comp.lang.c++
Path: i2pn2.org!i2pn.org!eternal-september.org!news.eternal-september.org!.POSTED!not-for-mail
From: andreytarasevich@hotmail.com (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$1@dont-email.me>
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: dont-email.me; posting-host="d1abbe6b301afbdecbb61dcc5dd31b91";
logging-data="4102142"; mail-complaints-to="abuse@eternal-september.org"; posting-account="U2FsdGVkX1+IwSAmNz07pg6LpLmLQa+l"
User-Agent: Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:102.0) Gecko/20100101
Thunderbird/102.14.0
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.

See https://godbolt.org/z/xxM7jn858

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
this

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
http://eel.is/c++draft/temp.spec#general-5.1). 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:
https://gcc.gnu.org/bugzilla/show_bug.cgi?id=87380. But this seems to be
too niche.

--
Best regards,
Andrey

1
server_pubkey.txt

rocksolid light 0.9.8
clearnet tor