Topic: Templates and code size (Was: Standard Template Library.)
Author: "Andrei Alexandrescu" <andrewalex@hotmail.com>
Date: 1999/10/30 Raw View
<vijayg@my-deja.com> wrote in message news:7v7muc$g8b$1@nnrp1.deja.com...
> Code bloat does seem to be an
> embarrasing problem - a few thousand lines
> of idiomatically written STL code can produce
> executables sized in MB.
Embarrasing to whom? To people who wrote the code? :o)
> I wonder if something like the STL
> could have been done without templates?
STL could not have been built with any other means that exist in the C++
language, than templates. STL leverages generic programming (GP). There is
so much C++ templates have to do with GP, and so few any other feature of
C++ has, that basically thinking of STL in C++ without templates doesn't
make any sense.
> In retrospect could C++ have been designed
> differently or better?
Sure. What I'm not sure, is whether you and I have a clue on improving its
design.
Andrei
---
[ 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 ]
[ --- Please see the FAQ before posting. --- ]
[ FAQ: http://reality.sgi.com/austern_mti/std-c++/faq.html ]
Author: vijayg@my-deja.com
Date: 1999/10/28 Raw View
Code bloat does seem to be an
embarrasing problem - a few thousand lines
of idiomatically written STL code can produce
executables sized in MB.
I wonder if something like the STL
could have been done without templates?
In retrospect could C++ have been designed
differently or better?
Sent via Deja.com http://www.deja.com/
Before you buy.
---
[ 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 ]
[ --- Please see the FAQ before posting. --- ]
[ FAQ: http://reality.sgi.com/austern_mti/std-c++/faq.html ]
Author: Jerry Leichter <jerrold.leichter@smarts.com>
Date: 1999/10/28 Raw View
| Note that conforming compilers: ...
| - even if they compile an instanciate [a template] in more than one
| translation unit, they shall only keep one instanciate
| in the final program....
Where in the standard does it say *that*? It's certainly not true, as
written, for in-lined functions.
More to the point, how could a Standard-conforming program ever know?
It's true that any two pointers to the same static member, or pointers-
to-member to the same non-static member, have to compare equal, but that
could be guaranteed in other ways. In fact, a common simple-
minded implementation model includes multiple instantiations in the
code, but only actually *uses* one of them. This requires less work
from the linker - it only has to play around with resolution of
pointers, something it's already doing, rather than rearrange code.
| Also note templates generally don't support separate compilation,
| so they are mostly incompatible with the very idea of DLL (except
| when instanting a template on a finite number of types, like char
| and wchar_t).
I'm not at all sure what you are trying to say here. However, consider
a shared library that includes an instantion of a particular template
instance. It can be used equally well with main programs that also
include instantiations of the same template instance, and those that
don't; it can be linked before or after the main program (most systems
provide a way to produce upward-compatible new versions of libraries
that don't require the main program to be rebuilt); and it can even be
loaded after the main program is running.
I know of no implementation that will avoid having two distinct copies
of the instantiation in memory when the main program has also
instantiated the particular instance. Most implementations do arrange
to use the code from only one of the copies, but eliminating the second
from memory would be very difficult. (In practice, there are other
things that are very hard to get right in a situation like this. For
example, static members of such instances are often constructed - and
destructed - multiple times if the library is loaded dynamically. Since
the Standard has nothing to say about dynamic loading, it's hard to call
this a violation, but it's something to watch out for.)
-- Jerry
---
[ 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 ]
[ --- Please see the FAQ before posting. --- ]
[ FAQ: http://reality.sgi.com/austern_mti/std-c++/faq.html ]
Author: Valentin Bonnard <Bonnard.V@wanadoo.fr>
Date: 1999/10/27 Raw View
Michael H. Cox wrote:
> It depends upon your compiler but look for some switches which control how
> template classes/functions are instantiated. On some compilers you can
> control whether it's per compilation unit or per executable. Also, some
> compilers allow you to specify if you want or don't want instantiation of
> member functions in templates that are not referenced. What's this last
> item for? Well, if you have an executable you may not want all functions
> instantiated; however, if you have a static library or shared/dynamic
> library you may want to have them instantiated 'cause you don't know who's
> going to need what.
Note that conforming compilers:
- shall not try to instanciate unused template functions
- even if they compile an instanciate in more than one
translation unit, they shall only keep one instanciate
in the final program
Also note templates generally don't support separate compilation,
so they are mostly incompatible with the very idea of DLL (except
when instanting a template on a finite number of types, like char
and wchar_t).
--
Valentin Bonnard
---
[ 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 ]
[ --- Please see the FAQ before posting. --- ]
[ FAQ: http://reality.sgi.com/austern_mti/std-c++/faq.html ]