Topic: Standard Template Library.
Author: JKB <jkb@halcyon.com>
Date: 1999/11/16 Raw View
Steve Clamage wrote:
> But if you are designing templates with [bloat] in mind...
> Write, for example, a non-template class vector_voidp for type void*.
> Then write a template wrapper vector that uses only inline forwarding
> functions with vector_voidp as its base. No code bloat.
>
This approach is also a way to push the template implementation
out of the header file down into an implementation file. Often the
void*-based implementation isn't a template at all. Even if it is, you
can explicitly instantiate it for the <void*> case in just one place.
-- jkb
[ Send an empty e-mail to c++-help@netlab.cs.rpi.edu for info ]
[ about comp.lang.c++.moderated. First time posters: do this! ]
[ 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: Darin Adler <darin@bentspoon.com>
Date: 1999/11/13 Raw View
Steve Clamage <stephen.clamage@sun.com> wrote:
> In principle, compilers could be smart enough to recognize when two
> template instantiations generate identical code, and use two labels
> to refer to one instantiation. Don't expect to see that any time soon.
I believe that Microsoft's Visual C++ linker does this optimization by
merging object modules with exactly the same object code (when debugging is
off enough and optimization is on enough). Can someone confirm or deny this?
-- Darin
---
[ 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: alexo@bigfoot---filter---.com (Alex Oren)
Date: 1999/11/10 Raw View
On 30 Oct 1999 06:02:48 -0400, Ross Smith <ross.s@ihug.co.nz> wrote:
} James Moe wrote:
} >
} > Vamsi Kalapala wrote:
} > >
} > > We want to use the standard template library for our work. But we are
} > > experiencing a code-bloat due to the instanciation of code for each .C
} > > file. Is this a common problem or is there anyway of avoiding code-bloat
} > > using templates.
} >
} > This is the great weakness of templates: for every new type
} > instantiation there is a whole new set of code to go with it to handle
} > that type.
}
} I've never been able to understand why people say this. In what way is
} that any kind of weakness of templates? Do you think the code would
} somehow magically become smaller if you wrote the same code for 10
} different types by hand, instead of instantiating a template 10 times?
No, but is the code for vector<int*> really that much different from the code
for vector<unsigned int*> to warrant duplication?
Have fun,
Alex.
--
My email address is intentionally mangled to foil spambots.
Please remove the "---filter---" from the address for replying.
Sorry for the inconvenience.
[ Send an empty e-mail to c++-help@netlab.cs.rpi.edu for info ]
[ about comp.lang.c++.moderated. First time posters: do this! ]
[ 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: Steve Clamage <stephen.clamage@sun.com>
Date: 1999/11/11 Raw View
Alex Oren wrote:
>
> On 30 Oct 1999 06:02:48 -0400, Ross Smith <ross.s@ihug.co.nz> wrote:
>
> } James Moe wrote:
> } >
> } > Vamsi Kalapala wrote:
> } > >
> } > > We want to use the standard template library for our work. But we are
> } > > experiencing a code-bloat due to the instanciation of code for each .C
> } > > file. Is this a common problem or is there anyway of avoiding code-bloat
> } > > using templates.
> } >
> } > This is the great weakness of templates: for every new type
> } > instantiation there is a whole new set of code to go with it to handle
> } > that type.
> }
> } I've never been able to understand why people say this. In what way is
> } that any kind of weakness of templates? Do you think the code would
> } somehow magically become smaller if you wrote the same code for 10
> } different types by hand, instead of instantiating a template 10 times?
>
> No, but is the code for vector<int*> really that much different from the code
> for vector<unsigned int*> to warrant duplication?
If you write your own Vector class for int* and want to use unsigned* with
it, you'd need to use a lot of casts. You can get the same effect
(writing a lot of casts) with the standard vector class.
But a better approach would be to write a wrapper Vector_unsigned
that just used inline forwarding functions to use the code of the
Vector class. There would be no overhead, and the casts would appear
only in the code for the Vector_unsigned functions.
You can do the same with templates: Write a vector_unsigned wrapper
that uses vector<int*>. Or, better, you could in principle write a
specialization vector<unsigned*> that uses the code of vector<int*>.
(For this particular case the standard says you can't write your own
specialization of the standard classes for built-in types.)
So what's the difference? The difference is that with templates you
can just use vector<int*> and vector<unsigned*> without thinking about
having to provide code to handle the new type, and wind up with "code
bloat".
But if you are designing templates with this problem in mind, you
can avoid the bloat entirely. Stroustrup shows how in C++PL.
Write, for example, a non-template class vector_voidp for type void*.
Then write a template wrapper vector that uses only inline forwarding
functions with vector_voidp as its base. No code bloat.
In principle, compilers could be smart enough to recognize when two
template instantiations generate identical code, and use two labels
to refer to one instantiation. Don't expect to see that any time soon.
--
Steve Clamage, stephen.clamage@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 ]
[ --- Please see the FAQ before posting. --- ]
[ FAQ: http://reality.sgi.com/austern_mti/std-c++/faq.html ]
Author: Francis Glassborow <francis@robinton.demon.co.uk>
Date: 1999/11/03 Raw View
In article <3818C5A2.21A947A3@ihug.co.nz>, Ross Smith
<ross.s@ihug.co.nz> writes
>I've never been able to understand why people say this. In what way is
>that any kind of weakness of templates? Do you think the code would
>somehow magically become smaller if you wrote the same code for 10
>different types by hand, instead of instantiating a template 10 times?
Indeed, and there are two other gains: you can extract type independent
code into a base class, the compiler should only instantiate those
member functions that you actually use.
I think the misconception arises out of earlier coding techniques using
void * etc. Most if all of these can be provided in a safer form by
using template with base class.
Francis Glassborow Journal Editor, Association of C & C++ Users
64 Southfield Rd
Oxford OX4 1PA +44(0)1865 246490
All opinions are mine and do not represent those of any organisation
[ Send an empty e-mail to c++-help@netlab.cs.rpi.edu for info ]
[ about comp.lang.c++.moderated. First time posters: do this! ]
[ 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: Ross Smith <ross.s@ihug.co.nz>
Date: 1999/10/30 Raw View
James Moe wrote:
>
> Vamsi Kalapala wrote:
> >
> > We want to use the standard template library for our work. But we are
> > experiencing a code-bloat due to the instanciation of code for each .C
> > file. Is this a common problem or is there anyway of avoiding code-bloat
> > using templates.
>
> This is the great weakness of templates: for every new type
> instantiation there is a whole new set of code to go with it to handle
> that type.
I've never been able to understand why people say this. In what way is
that any kind of weakness of templates? Do you think the code would
somehow magically become smaller if you wrote the same code for 10
different types by hand, instead of instantiating a template 10 times?
--
Ross Smith <ross.s@ihug.co.nz> The Internet Group, Auckland, New Zealand
========================================================================
"There are many technical details that make Linux attractive to the
sort of people to whom technical details are attractive." -- Suck
---
[ 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: "Ron" <nospam@this-address.org>
Date: 1999/10/30 Raw View
James Moe <sma.spam-not@rtd.com> wrote in message
news:3816A0E8.CC83F841@rtd.com...
> Vamsi Kalapala wrote:
> >
> > We want to use the standard template library for our work. But we are
> > experiencing a code-bloat due to the instanciation of code for each .C
> > file. Is this a common problem or is there anyway of avoiding code-bloat
> > using templates.
>
> This is the great weakness of templates: for every new type
> instantiation there is a whole new set of code to go with it to handle
> that type.
The STL vendor can do a lot to reduce code bloat. They could, for example,
create an STL whose header files are merely type-safe wrappers around an
"engine", of which there exists only one copy. A vendor named "Deltalogic"
(www.deltalogic.com) has done this. Their STL is, however, quite expensive.
-- Ron
---
[ 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: "Robert O'Dowd" <nospam@nonexistant.com>
Date: 1999/11/01 Raw View
James Moe wrote:
>
> Vamsi Kalapala wrote:
> >
> > We want to use the standard template library for our work. But we are
> > experiencing a code-bloat due to the instanciation of code for each .C
> > file. Is this a common problem or is there anyway of avoiding code-bloat
> > using templates.
>
> This is the great weakness of templates: for every new type
> instantiation there is a whole new set of code to go with it to handle
> that type.
>
There are two issues here.
Some compilers are rather weak in their handling of templates.
For example, if a given template instantiation is used in two separate
source files, a number of compilers will duplicate the code in
both object files.
For example
template <class T>
void Swap(T &a, T &b)
{
T temp = a;
a = b;
b = temp;
}
used in source1.cc via
double a = something, b = something_else;
Swap(a,b);
and separately in source2.cc
double a = something, b = something_else;
Swap(a,b);
will instantiate the template in both source1.o and source2.o.
It is essentially the same as the Swap function being used
as static functions local to each source file. However,
it means (effectively) two copies of the Swap(double &, double &)
function in the executable.
More advanced compilers support things like explicit instantiation
and various compiler options to prevent the above happening.
Some (more recent) compilers even do that by default. To work
around this sort of "code bloat" you need to look at the
documentation for your compiler.
The second issue is that the compiler is usually responsible for
instantiating two versions of Swap in the following.
double a = something, b = something_else;
int ai = isomething, bi = isomething_else;
Swap(a,b);
Swap(ai, bi);
Many programmers seems to expect that this will make the executable
magically smaller than the use of two separately hand crafted versions
of Swap(). That is an unrealistic expectation in many cases.
It would, almost certainly, require a lot of intelligence from
the program linker in addition to the compiler. Most systems
do not have such a smart linker.
-<Automagically included trailer>
Robert O'Dowd Ph +61 (8) 8259 6546
MOD/DSTO Fax +61 (8) 8259 5139
P.O. Box 1500 Email:
robert.odowd@dsto.defence.gov.au
Salisbury, South Australia, 5108
Disclaimer: Opinions above are mine and may be worth what you paid for
them
---
[ 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: rabins@my-deja.com
Date: 1999/10/30 Raw View
Two more peace of info:
gcc-2.7.x bares the problem you mentioned while gcc-2.8 and egcs have
overcome it.
Here is an uglly way to overcome the problem, defing a new master.cc
file which containes lines such as
#include "xxx1.cc"
and compile the master file instadd of the proper cc files.
It have many disadvanteges such as Makefile triggers and symbols
collisions.
But it have also several advanteges:
usealy overcome compile time is decreased.
The compiler optimization space is bigger.
And the wanted effect: the size of the object is small!
>In article <381481F4.40784DE5@arc.unm.edu>,
> Vamsi Kalapala <vamsi@arc.unm.edu> wrote:
> We want to use the standard template library for our work. But we are
> experiencing a code-bloat due to the instanciation of code for each .C
> file. Is this a common problem or is there anyway of avoiding
code-bloat
> using templates.
Sent via Deja.com http://www.deja.com/
Before you buy.
[ Send an empty e-mail to c++-help@netlab.cs.rpi.edu for info ]
[ about comp.lang.c++.moderated. First time posters: do this! ]
[ 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: "Ken Hagan" <K.Hagan@thermoteknix.co.uk>
Date: 1999/10/30 Raw View
Vamsi Kalapala wrote in message <381481F4.40784DE5@arc.unm.edu>...
>We want to use the standard template library for our work. But we are
>experiencing a code-bloat due to the instanciation of code for each .C
>file. Is this a common problem or is there anyway of avoiding code-bloat
>using templates.
It is a common problem amongst users of compilers that don't support
partial specialisation for things like vector<T*>, or which instantiate too
much code and fail to discard the excess during the link phase. :-)
For "real" compilers, I think the idea is that the instantiated templates
need never add up to much more than the hand coded alternative. (After
all, they don't *do* any more.) Since I use MSVC, I'm in no position to
know how closely current implementations approach the ideal.
[ Send an empty e-mail to c++-help@netlab.cs.rpi.edu for info ]
[ about comp.lang.c++.moderated. First time posters: do this! ]
[ 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: "Michael H. Cox" <mhcox@crosskeys.com>
Date: 1999/10/27 Raw View
Vamsi Kalapala wrote:
> We want to use the standard template library for our work. But we are
> experiencing a code-bloat due to the instanciation of code for each .C
> file. Is this a common problem or is there anyway of avoiding code-bloat
> using templates.
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.
Michael
[ Send an empty e-mail to c++-help@netlab.cs.rpi.edu for info ]
[ about comp.lang.c++.moderated. First time posters: do this! ]
[ 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: James Moe <sma.spam-not@rtd.com>
Date: 1999/10/28 Raw View
Vamsi Kalapala wrote:
>
> We want to use the standard template library for our work. But we are
> experiencing a code-bloat due to the instanciation of code for each .C
> file. Is this a common problem or is there anyway of avoiding code-bloat
> using templates.
This is the great weakness of templates: for every new type
instantiation there is a whole new set of code to go with it to handle
that type.
--
sma at rtd dot com
Remove ".spam-not" for email
[ Send an empty e-mail to c++-help@netlab.cs.rpi.edu for info ]
[ about comp.lang.c++.moderated. First time posters: do this! ]
[ 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: acurio@my-deja.com
Date: 1999/10/28 Raw View
What compiler are you using? We use stl extensively with a fairly
recent version of gcc. It does make the overall executable 10%-20%
larger and 10%-20% slower in compilation time (and overall faster at
run time, sometimes several times faster because better inlining in
algorthms like sort), but I won't call it a bloat yet...
A.
In article <381481F4.40784DE5@arc.unm.edu>,
Vamsi Kalapala <vamsi@arc.unm.edu> wrote:
> We want to use the standard template library for our work. But we are
> experiencing a code-bloat due to the instanciation of code for each .C
> file. Is this a common problem or is there anyway of avoiding code-
bloat
> using templates.
Sent via Deja.com http://www.deja.com/
Before you buy.
[ Send an empty e-mail to c++-help@netlab.cs.rpi.edu for info ]
[ about comp.lang.c++.moderated. First time posters: do this! ]
[ 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: Vamsi Kalapala <vamsi@arc.unm.edu>
Date: 1999/10/26 Raw View
We want to use the standard template library for our work. But we are
experiencing a code-bloat due to the instanciation of code for each .C
file. Is this a common problem or is there anyway of avoiding code-bloat
using templates.
---
[ 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: comeau@panix.com (Greg Comeau)
Date: 1999/10/27 Raw View
In article <381481F4.40784DE5@arc.unm.edu> Vamsi Kalapala <vamsi@arc.unm.edu>
writes:
>We want to use the standard template library for our work. But we are
>experiencing a code-bloat due to the instanciation of code for each .C
>file. Is this a common problem or is there anyway of avoiding code-bloat
>using templates.
Some things you want to consider first:
1) See if you can change the "granularity of instantiation" with
your compiler. (IOWs, do you instantiate the world for every .C?)
2) Does the bloat remain just in the object file or does it go
away in the executable?
- Greg
--
Comeau Computing, 91-34 120th Street, Richmond Hill, NY, 11418-3214
Producers of Comeau C/C++ 4.2.38 -- NOTE 4.2.42 BETAS NOW AVAILABLE
Email: comeau@comeaucomputing.com / Voice:718-945-0009 / Fax:718-441-2310
*** WEB: http://www.comeaucomputing.com ***
[ Send an empty e-mail to c++-help@netlab.cs.rpi.edu for info ]
[ about comp.lang.c++.moderated. First time posters: do this! ]
[ 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 ]