Alex Rivera | Logout

When are header-only libraries acceptable?

Asked 2010-02-01T04:29:42.783
30

Personally, I quite like header-only libraries, but there are claims they cause code bloat due to over-inlining (as well as the other obvious problem of longer compile times).

I was wondering, how much truth is there to these claims (the one about bloat)?

Furthermore, are the costs 'justified'? (Obviously there are unavoidable cases such as when it's a library implemented purely or mostly with templates, however I'm more interested in the case where there's actually a choice available.)

I know there's no hard and fast rule, guideline, etc as far as stuff like this goes, but I'm just trying to get a feel for what others think on the issue.

P.S. Yes this is a very vague and subjective question, I'm aware, and so I have tagged it as such.

Edit
Report

1 Answer

10

In my experience bloat hasn't been a problem:

  • Header only libraries give compilers greater ability to inline, but they do not force compilers to inline - many compilers treat the inline keyword as nothing more than a command to ignore multiple identical definitions.

  • Compilers usually have options to optimize to control the amount of inlining; /Os on Microsoft's compilers.

  • It's usually better to allow the compiler to manage the speed vs. size issues. You'll only see bloat from calls that have actually been inlined, and the compiler will only inline them if its heuristics indicate that it inlining will improve performance.

I wouldn't consider code bloat as a reason to stay away from header only libraries - but I would urge you to consider how much a header only approach will increase compile times by.

answered 2010-02-01T11:58:07.300

Your Answer