|
 |
Warp wrote:
> Darren New <dne### [at] san rr com> wrote:
>>> If you change T.chh, then A.cpp - and any other source file that
>>> includes T.chh - must be recompiled.
>
>> And this is why Java doesn't do templates. You may not have the source
>> code for A.cpp.
>
> You mean that if the public interface of a class T changes, nevertheless
> the class A which uses T doesn't need to be recompiled? How does it manage
> to do that?
Dynamic loading. :-)
Seriously, tho, I wasn't necessarily talking about the public interface.
I was talking about the implementation of the functions. In Java, the
code for the generic isn't duplicated for different types, and it gets
linked in at runtime. Hence, the whole question of "what if it changes"
doesn't matter - you compile against the types, not against the bodies
of the code. If you change the public interface to the point where the
old code won't work, then when you try to instantiate the generic, it'll
tell you that code couldn't be dynamically loaded. I.e., it falls back
to being a run-time error, just like if you try to load a .so / .dll and
that isn't present on the disk. (Which I guess is in some ways worse
than what you get with C++ and static linking. :-)
> After all, the T.hh header might just contain the public interface for
> the template code. Its implementation may be in its own compilation unit
> (if the compiler supports fully the C++ standard).
The whole "export template" bit I don't understand. I have to believe
that either there are restrictions on what a template can do and still
be able to be an export template, or there are bits in the object file
which are essentially getting recompiled at compile-time.
How does it work if the template class declares in one of the functions
an array of type T (where T is one of the generic parameters), and then
indexes that with []? If T overrides [], it has to call different
things for each T, and if T doesn't override [], it has to use different
multipliers based on the size of each T object, yes? If you're not doing
that, then you have generics instead of templates, where the same code
is getting used and when you "instantiate" it, it fills in blanks like
"pointer to function that does indexing, size of one element of the
array", etc?
I've even seen purposely-obtuse C++ template stuff that either declares
a type or a function (both with the same name) depending on the size of
the generic type passed in or some such, which would seem to be
something really difficult to put into object code that doesn't have to
get recompiled when you instantiate it.
--
Darren New / San Diego, CA, USA (PST)
"That's pretty. Where's that?"
"It's the Age of Channelwood."
"We should go there on vacation some time."
Post a reply to this message
|
 |