Boost logo

Boost Users :

From: Steve Horne (steve_at_[hidden])
Date: 2003-09-16 14:51:15


At 23:25 16/09/2003 +0800, you wrote:
>Hi All,
>I am a new user of Boost. but when I look the source of Boost, I found
>that almost all the implementations is put in head files together, not as
>usual to
>put into CPP file. I know, it must be intended, however, could it possible
>to move the implementations into cpp file in future?
>
>Sorry if this question has been posted.

Cool - a question that I can answer!

The boost libraries make heavy use of templates. To be useful, the
templates must be 'instantiated' by the library user - ie the template
parameters supplied and the resulting object code generated. In a sense,
this is rather like using a macro to generate the code.

As the code has to be available when the users code instantiates it, it has
to be kept in header files. There is no resultant object code until the
instantiation is compiled.

For example, every cpp file where a particular template is instantiated
with the same parameters will contain a duplicate copy of the resultant
object code. This is handled by the linker, which discards duplicate copies
of that object code so they don't increase the size of the application.

This approach to templates can result in 'bloat', however, if templates do
not share code with other instantiations (instantiated with different
parameters) when possible. This is normally solved by writing templates
that provide typesafe wrappers to non-template underlying code and data
structures. When this is done, the template instantiation itself generates
very little object code.

It is also not unusual for templates to not generate object code at all,
but to effectively evaluate expressions at compile-time - so-called
template metaprogramming.

The point of all this is basically this - to the best of my (very limited)
knowledge, Boost already uses CPP files wherever that would be appropriate.


Boost-users list run by williamkempf at hotmail.com, kalb at libertysoft.com, bjorn.karlsson at readsoft.com, gregod at cs.rpi.edu, wekempf at cox.net