tags:

views:

158

answers:

3

I've noticed that when I use a boost feature the app size tends to increase by about .1 - .3 MB. This may not seem like much, but compared to using other external libraries it is (for me at least). Why is this?

+2  A: 

Boost uses templates everywhere. These templates can be instantiated multiple times with the same parameters. A sufficiently smart linker will throw out all but one copy. However, not all linkers are sufficiently smart. Also, templates are instantiated implicitly sometimes and it's hard to even know how many times one has been instantiated.

dsimcha
"not all linkers are sufficiently smart". Being so dumb as to leave in "multiple copies" of the same code is also sometimes referred to as "inlining" ;-). Seriously, though, templates are easier to inline than static libraries. Unless you set the options to only inline when it won't increase the code size, it'll increase the code size. That's even before any straight-up inefficiencies like duplicated out-of-line code.
Steve Jessop
You will get a copy in every single obj file that references each header, as well. Since most of these templates are implemented on top of other templates, multiple each template by the number of templates it uses as well. You can easily get tens of thousands of duplicates of cout templates alone in a big project. And the way stl and boost are implemented, code is monstrously bloated anyhow. Plus mem fragmentation and optimization killing happens like crazy because there's so many func ptrs and smt ptrs. In short, don't use boost at all, and use stl as sparingly as possible.
Charles Eli Cheese
@Charles: Absolutely terrible general advice. Unless you specifically mean to lower code size (which seriously doesn't matter much anymore), you should use them as much as possible.
GMan
A: 

It all depends on how it is used. Since Boost is a bunch of templates, it causes a bunch of member functions to be compiled per type used. If you use boost with n types, the member functions are defined (by C++ templates) n times, one for each type.

wallyk
+1  A: 

"so much" is a comparative term, and I'm afraid you're comparing apples to oranges. Just because other libraries are smaller doesn't imply you should assume Boost is as small. Look at the sheer amount of work Boost does for you!

I doubt making a custom library with the same functionality would be of any considerable lesser size. The only valid comparison to make is "Boost's library that does X" versus "Another library that does X". Not "Boost's library that does X" and "Another library that does Y."

The file system library is very powerful, and this means lots of functions, and lot's of back-bone code to provide you and I with a simple interface. Also, like others mentioned templates in general can increase code size, but it's not like that's an avoidable thing. Templates or hand-coded, either one will results in the same size code. The only difference is templates are much easier.

GMan