Boost logo

Boost :

From: Christoph Ludwig (cludwig_at_[hidden])
Date: 2005-06-29 08:47:40


On Wed, Jun 29, 2005 at 02:52:12PM +0200, Markus Schöpflin wrote:
> Christoph Ludwig wrote:
>
> > On Wed, Jun 29, 2005 at 12:59:57PM +0100, John Maddock wrote:
> >
> >>>Umm, now I'm confused. Why is explicit instantiation in more than one
> >>>translation unit illegal?
> >>
> >>I think I must be loosing it.... I can't find any such prohibition
> >>anywhere.... I was so sure that I'd had problems with this in the past as
> >>well!
>
> > You are referring to the explicit instantiation of the same template
> > specialization in several TUs, aren't you?
> >
> > That is indeed prohibited. I am too lazy to look for chapter and verse in my
> > copy of the standard but Vandevoorde and Josuttis write in "C++ Templates. The
> > Complete Guide":
> >
> > Section 6.2.1:
> > There should be, at most, one explicit instantiation of each distinct entity
> > in a program. [...] Not following this rule usually results in linker errors
> > that report duplicate definitions of the instantiated entities.
>
> The problem is that the compiler _needs_ the explicit instantiation in
> order not to end up with multiple defitions of a static variable used in
> the class. And where should that instantiation be written, if not in the
> header?

I don't know the peculiarities of the Tru64/CXX compiler. If it does not care
about multiple explicit instantiations (or even requires them for some
reason), then it may make sense to ignore the standard *on this particular
platform*. The explicit instantiation should be guarded by #ifdefs that filter
out all other platforms, though.

> > Section 10.5:
> > The standard also specifies that there can be at most one explicit
> > instantiation of a certain template specialization in a
> > program. Furthermore, if a template specialization is explicitly
> > instantiated, it should not be explicitly specialized, and vice versa.
>
> I can't find anything in the standard (14.7) indicating this.

OK, I overcame my laziness :-) It is 14.7p5:

  No program shall explicitly instantiate any template more than once, both
  explicitly instantiate and explicitly specialize a template, or specialize a
  template more than once for a given set of template arguments.
  An implementation is not required to diagnose a violation of this rule.

I admit I find Vandvoorde's and Josuttis' phrasing much easier to grok.

Regards

Christoph

-- 
http://www.informatik.tu-darmstadt.de/TI/Mitarbeiter/cludwig.html
LiDIA: http://www.informatik.tu-darmstadt.de/TI/LiDIA/Welcome.html

Boost list run by bdawes at acm.org, gregod at cs.rpi.edu, cpdaniel at pacbell.net, john at johnmaddock.co.uk