talking about the way to organize template source code

F

feverzsj

Since the separation model seems make nothing cheap, here ,we only
talk about the file organization under the inclusion model.
IMO, the way to organize template source code should try to achieve
two goals, a clear interface and minimizing the compilation
dependences and time if possible.
It is aways easy to archive a "clear look" interface, using a
interface/impl idiom with a impl data member or or impl base or
anything possible. But include them all before the instantiation
point.
For the 2nd goal, forgive my ignorance, I can only figure out two
basic points, lazy instantiation and specialization. Lazy
instantiation is related to dependent name. For example, if the impl
part is related to dependent name, we can offer the definition until
the instantiation point. As for specialization, clearly, it helps to
decrease the compilation time and also the code size(use with the
partial specialization). The explicit instantiation also helps to
decrease the compilation time, but it makes little sense to generic
programming.
Combining all of them, it becomes much tricky(e.g. the pimpl of
templte) to achieve both goals. We have to design the template
architecture carefully, make a appropriate granularity of abstraction
to benefit the lazy instantiation and specialization...
This post is yet a very crude overview on the organization of
template source code, I'll add some detailed idiom and example later.
Also, I hope you can post your notion, suggestion, example and any
related comments.
 
V

Vladimir Jovic

feverzsj said:
Since the separation model seems make nothing cheap, here ,we only
talk about the file organization under the inclusion model.
IMO, the way to organize template source code should try to achieve
two goals, a clear interface and minimizing the compilation
dependences and time if possible.
It is aways easy to archive a "clear look" interface, using a
interface/impl idiom with a impl data member or or impl base or
anything possible. But include them all before the instantiation
point.

The pimpl idiom can be used regardless whether the class is a template
or not.

What is "impl base"?

For the 2nd goal, forgive my ignorance, I can only figure out two
basic points, lazy instantiation and specialization. Lazy
instantiation is related to dependent name. For example, if the impl
part is related to dependent name, we can offer the definition until
the instantiation point. As for specialization, clearly, it helps to
decrease the compilation time and also the code size(use with the
partial specialization). The explicit instantiation also helps to
decrease the compilation time, but it makes little sense to generic
programming.

I never measured or checked, but how bad is a template instantiation?
Computers became very fast, therefore template instantiations should be
fast, no?
Also, the size. Who cares if a class occupies few Ks more or less?
Combining all of them, it becomes much tricky(e.g. the pimpl of
templte) to achieve both goals. We have to design the template
architecture carefully, make a appropriate granularity of abstraction
to benefit the lazy instantiation and specialization...
This post is yet a very crude overview on the organization of
template source code, I'll add some detailed idiom and example later.
Also, I hope you can post your notion, suggestion, example and any
related comments.

This post is bit confusing. An example would be great.
 
J

James Kanze

The pimpl idiom can be used regardless whether the class is a template
or not.

In fact, unless your compiler supports export, there's
absolutely no sense in using it with templates. The whole point
of the compilation firewall idiom is to remove dependencies from
the header files.
What is "impl base"?
I never measured or checked, but how bad is a template
instantiation? Computers became very fast, therefore template
instantiations should be fast, no? Also, the size. Who cares
if a class occupies few Ks more or less?

Computers are very fast, but disks and networks haven't followed
in the same proportion. Instantiating a template isn't
necessarily that expensive, but having to read the source code
to do so, for each of the translation units which includes the
template header, can be expensive. And having to recompile each
of the translation units which uses the template, just because
you modified some small implementation detail, can be very
expensive.
This post is bit confusing. An example would be great.

I wasn't too sure what he was getting at either. Maybe the idea
of factoring much of the common behavior out into a non-template
base class?
 

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

Forum statistics

Threads
473,982
Messages
2,570,190
Members
46,736
Latest member
zacharyharris

Latest Threads

Top