Some C++0x question - extern template

S

Stephen Horne

Can the same template with the same parameters be both forcibly
instantiated and extern-templated in the same compilation unit? If so,
which takes precedence.

For example, it might make sense to have a header which declares a
template and then uses extern template for all the common
instantiations, and an implementation file for the library that
#includes that header then overrides all those extern template lines
to force the instantiation of those templates. The idea is to localise
the instantiation of these common cases to the library, preventing
users of that library from re-doing that work.
 
S

SG

Can the same template with the same parameters be both forcibly
instantiated and extern-templated in the same compilation unit?

I think the answer is yes. From what I understand an "explicit
template instantiation definition" can be preceeded by an "explicit
template instantiation declaration".
For example, it might make sense to have a header which declares a
template and then uses extern template for all the common
instantiations, and an implementation file for the library that
#includes that header then overrides all those extern template lines
to force the instantiation of those templates. The idea is to localise
the instantiation of these common cases to the library, preventing
users of that library from re-doing that work.

I think it is the extern template proposal's intention to allow
exactly what you have described.

Cheers!
SG
 
A

Alan Woodland

SG said:
I think the answer is yes. From what I understand an "explicit
template instantiation definition" can be preceeded by an "explicit
template instantiation declaration".


I think it is the extern template proposal's intention to allow
exactly what you have described.

Does this now prohibit inlining within a TU though? Or is the compiler
still free to decide to inline things here as it pleases?

(And incidentally do any current compilers actually still inline if
they're permitted to? It seems like it would be quite hard for them to
decide to inline without losing a fair chunk of the benefits of the
extern template surely?)

Alan
 
B

Bo Persson

Alan said:
Does this now prohibit inlining within a TU though?

No, not at all. At worst it makes it a little more difficult.
Or is the
compiler still free to decide to inline things here as it pleases?

As long as you (=a conforming program) can't tell the difference, the
compiler can do anything it likes.
(And incidentally do any current compilers actually still inline if
they're permitted to? It seems like it would be quite hard for them
to decide to inline without losing a fair chunk of the benefits of
the extern template surely?)

Some compilers already inline functions across compilation units. It
"just" has to link first, and decide on inlining afterwards. This
shouldn't be much harder to do for extern templates.


Bo Persson
 
S

Stephen Horne

Some compilers already inline functions across compilation units. It
"just" has to link first, and decide on inlining afterwards. This
shouldn't be much harder to do for extern templates.

Lambda the Ultimate had a thing about linkers recently, describing
them as declarative programming languages.

http://lambda-the-ultimate.org/node/3474

Lots of interesting info in the links. It reminded me of working with
Ada for embedded stuff in the mid to late 90s (using Ada 83, since the
95 standard had only recently been finalised).

Basically, all the code was compiled into a kind of database, which I
always assumed held an intermediate representation other than native
machine code. A second compilation phase was applied to this
intermediate representation.

Unfortunately, I never was clear on exactly what was going on. I have
the impression now that Ada 83 generics were probably compiled (at
least to that intermediate representation) directly in that first
phase, using a closure system for instantance parameters. That rules
out a lot of inlining, of course.

Anyway, the point is that it does look like that the distinction
between the compiler back-end and the linker is blurring over time,
maybe to the point that they'll eventually merge, allowing the
compiler to either do compilation early or delay it as appropriate for
each chunk of code. And if Ada 83 was already deviating from the
traditional compile-then-link model, then that evolution has been
happening for a long time.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
474,159
Messages
2,570,879
Members
47,416
Latest member
LionelQ387

Latest Threads

Top