|
 |
Warp wrote:
> Darren New <dne### [at] san rr com> wrote:
>> For example, you have to parse and compile the
>> templates each time from source
>
> A compiler which supports export templates could conceivably have
> precompiled templates for certain (although obviously not all) types,
> such as for example for all basic types and STL classes. Thus it could
> avoid having to compile them each time.
Yes. If you restrict templates to doing what generics do, you don't have
to compile them each time. But that's a restriction on templates.
>> and you get a new blob of code for each
>> type you instantiate a template with.
>
> If you are using the same code with two different types you are obviously
> going to get two different pieces of code, no matter how you do it.
Not in every language, no. Depending on what you do with the types,
yes. That, for example, *is* one difference between generics and
templates. Obviously Java "generics" (such as they are) don't have this
restriction, because by the time you get to object code, all the actual
type information is gone.
In any case, what *I* meant was that you get a new blob of code compiled
every time you instantiate a template. Not for each different type, but
even for the same types in different compilation units. Yes? Or am I
confused about that? If I have std:vector<MyType> having functions
called in one module, then I compile another module with
std:vector<MyType>, does the linker see that it's the same code and
function on the same type and only include one copy of the object code
in the linked result?
> Templates just make it easier to avoid code repetition. (In other words,
> if you want, for example, a vector of ints and a vector of doubles, you
> don't need to write the vector code twice.)
Right. Generics do the same thing. They're just more restricted than
what templates give you.
> Templated data containers in C++ usually produce more memory-efficient
> code than equivalent data containers using the pointer-to-base-Object
> mechanism, which usually wastes a lot of memory.
Unless everything's already a pointer-to-base-Object, as you already
decry. ;-)
> Thus any argument that
> templates cause memory bloat is usually false. (Yes, there may be a few
> situations where templated code causes more memory usage, but normally
> it's the opposite.)
I think they likely cause code file bloat. If you use the same template
and instantiate it on 10 different types of pointers, aren't you going
to get ten different copies of object code?
>> Now, "crippled" is what I'd call the baroque C++ syntax.
> Syntax doesn't really matter all that much. It's how you use it.
True. It's a pretty minor thing, once you get used to it. I just dislike
people taking the syntax into languages that don't need it. Right up
there with people who think a leading zero means octal is a good idea on
values rather than constants.
--
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
|
 |