This is the mail archive of the
gcc-help@gcc.gnu.org
mailing list for the GCC project.
Re: Explicit instantiation and static objects in different modules
- From: Alexander Monakov <amonakov at ispras dot ru>
- To: Jonathan Wakely <jwakely dot gcc at gmail dot com>
- Cc: Nikolay Vorobyov <nik dot vorobyov at gmail dot com>, gcc-help <gcc-help at gcc dot gnu dot org>
- Date: Tue, 4 Aug 2015 20:32:31 +0300 (MSK)
- Subject: Re: Explicit instantiation and static objects in different modules
- Authentication-results: sourceware.org; auth=none
- References: <63FDE6C4-EFC2-44DD-991E-0BE895586162 at gmail dot com> <CAH6eHdRy3NWN1xC9DfHx0UKmAQByGHtJfiWTYdssfqa-75NLGw at mail dot gmail dot com> <alpine dot LNX dot 2 dot 20 dot 1508041934440 dot 28968 at monopod dot intra dot ispras dot ru> <CAH6eHdR4sp2TCPHHu7Bn+XD-0O0YZSVQ2QHtBKDcA+wPskSYaQ at mail dot gmail dot com>
On Tue, 4 Aug 2015, Jonathan Wakely wrote:
> The extern template tells the compiler it doesn't *need* to
> instantiate the template, because it will be explicitly instantiated
> in another translation unit. But that doesn't mean it *must not*
> instantiate it. The compiler can choose to inline the function and in
> that case it will implicitly instantiate it. That should be
> unobservable because the One Definition Rule means that the implicitly
> instantiated definition that gets inlined and the explicitly
> instantiation definition in the other translation unit must be
> identical. The Windows linkage model seems to break that assumption
> ... so I don't know what the right behaviour is.
OK, thanks. AFAIU, a very similar problem existed with GCC on Linux prior to
introduction of STB_GNU_UNIQUE (consider what happened when multiple DSOs with
a common singleton are loaded with RTLD_LOCAL -- they didn't have a way to end
up with a common location).
In light of that, perhaps it's best to structure code in a way that prevents
implementation of a singleton from being visible in translation units that can
be linked into different libraries.
Alexander