Topic: Precompiling templates to highly decrease
Author: clamage@Eng.Sun.COM (Steve Clamage)
Date: 1996/07/24 Raw View
In article 64B415D1@ims.gso.getronics.nl,
Carlo <carlo@ims.gso.getronics.nl> writes:
>
>To start with one of these things: it would really be logical if template
>parameters could be restricted to base class types.
>
>Assume we have a base class:
>
>class B { /* ... */ };
>
>>>> Then a template definition could be:
>
>template < class B::T > // Just making up some syntax here, T should be 'a B'
> // (either B, or derived from B).
>class A {
> // ...
>};
...
>However, many practical classes only use pointers to a type:
>
>template < class T >
>class A {
> T *foo;
> // ...
>};
>
>This *could* be precompiled. Just replace the pointer (T*) by (void*), and
>you get:
>
>class A {
> void *foo;
> // ...
>};
"The Design and Evolution of C++" by Stroustrup contains a detailed
discussion and analysis of this and related proposals. Either there
or in "The C++ Programming Language" he shows how to use void*
implementations of pointer templates to prevent code explosion. (In
effect giving you precompiled templates.) The Rogue Wave libraries currently
use that technique.
---
Steve Clamage, stephen.clamage@eng.sun.com
---
[ comp.std.c++ is moderated. To submit articles: try just posting with ]
[ your news-reader. If that fails, use mailto:std-c++@ncar.ucar.edu ]
[ FAQ: http://reality.sgi.com/employees/austern_mti/std-c++/faq.html ]
[ Policy: http://reality.sgi.com/employees/austern_mti/std-c++/policy.html ]
[ Comments? mailto:std-c++-request@ncar.ucar.edu ]
Author: Carlo <carlo@ims.gso.getronics.nl>
Date: 1996/07/25 Raw View
Steve Clamage wrote:
> >However, many practical classes only use pointers to a type:
> >
> >template < class T >
> >class A {
> > T *foo;
> > // ...
> >};
> >
> >This *could* be precompiled. Just replace the pointer (T*) by (void*), and
> >you get:
> >
> >class A {
> > void *foo;
> > // ...
> >};
>
> "The Design and Evolution of C++" by Stroustrup contains a detailed
> discussion and analysis of this and related proposals. Either there
> or in "The C++ Programming Language" he shows how to use void*
> implementations of pointer templates to prevent code explosion. (In
> effect giving you precompiled templates.) The Rogue Wave libraries currently
> use that technique.
Unfortunately I do not have "The Design and Evolution of C++", but
I think I know which tecnique you are refering to since this is what
I am doing myself already ;).
As soon as I have a template with a non inline method:
template < class T > // T must be a `B'
class A {
T a;
public:
T &non_inline_method(T &foo);
T &inline_method(void) { return a; }
};
I split it up into a non-template base class and a template:
class A_base {
public:
B *non_inline_method(B &foo);
};
template < class T > // T must be a `B'
class A : public A_base {
friend class A_base;
T a;
public:
T &non_inline_method(T &foo)
{ return *(T*)A_base::non_inline_method(foo); }
T &inline_method(void) { return a; }
};
It is however rather dirty that I have to use casting, which has given
me problems several times. A programmer should not have to take
care about optimisation in this way. There are also several practical
problems that could be better avoided when the compiler would do this
optimisation (and at least the programmer would not have to worry about
it himself anymore).
The only extention to the standard would be to allow to define of what
base class a template parameter has to be (like above, where T must be
a B). That would be a change that at least *allows* to do compiler
optimization in this field in the future.
Carlo
--
---------------------------------------------------------------------------
Junior Technology Expert | Getronics Software (http://gso.getronics.nl)
E-mail: carlo@ims.gso.getronics.nl | Tel.nr: +31-20-4306519
---
[ comp.std.c++ is moderated. To submit articles: try just posting with ]
[ your news-reader. If that fails, use mailto:std-c++@ncar.ucar.edu ]
[ FAQ: http://reality.sgi.com/employees/austern_mti/std-c++/faq.html ]
[ Policy: http://reality.sgi.com/employees/austern_mti/std-c++/policy.html ]
[ Comments? mailto:std-c++-request@ncar.ucar.edu ]