Topic: Templates and Undefined Functions.


Author: vandevod@cs.rpi.edu (David Vandevoorde)
Date: 1995/10/09
Raw View
>>>>> "HS" == Herb Sutter <herbs@interlog.com> writes:
HS> In article <30753E6F@Toronto.LinkAge.com>, Harvey Rook
HS> <harveyr@LinkAge.com> wrote:
>> What does the standard say about templates and inline functions
>> that are never used? Do they have to contain completly valid code?
>> Or can they be ignored? Which compiler is right?

HS> Template code that is never called should not be generated --
HS> whether inline or out-of-line.  Not only does this mean that
HS> classes like the one you showed (one which is never instantiated)
HS> should never get code generated, but even classes which are
HS> instantiated should only have those templated members instantiated

Non-virtual members, that is. Virtual members may be emitted even
though they're never called (because it's so hard to predict whether
they're actually called or not).

HS> that are actually used.  In other words, you can have a class with
HS> some function that contains illegal code (either always illegal,
HS> or illegal to instantiate for a particular type), and the compiler

Actually, the code must be ``grammatically correct''; i.e., you
you do not have to guarantee things like accessibility, but you
must provide correct syntax.

HS> should not generate the code if it is not called.  Incidentally,
HS> this feature is quite useful... not just because is saves dead
HS> code, but because it gives you some flexibility with a template's
HS> type restraints if you don't plan to use all features of the
HS> template.

Very true!

HS> As you've noticed, though, compilers are always playing catchup
HS> with the draft and not all have even basic templates solidly
HS> implemented yet.  For example, I would be surprised if Visual C++
HS> supported the above requirement (from all that I've heard their
HS> template code is still weak... won't run a complete STL
HS> implementation, for example).

Yes, many compilers --- even some that are good enough to handle
HP's STL --- still attempt to instantiate inline-members of
template classes whether used ot not. And a pain it is...
(Check my valarray-source for sample-contortions :)

 Daveed



---
[ comp.std.c++ is moderated.  Submission address: std-c++@ncar.ucar.edu.
  Contact address: std-c++-request@ncar.ucar.edu.  The moderation policy
  is summarized in http://dogbert.lbl.gov/~matt/std-c++/policy.html. ]