Boost Library [closed]

Since I have started using this site, I keep hearing about the Boost library. I am wondering what are some of the major benefits of the Boost library (hence why should I use it) and how portable is the Boost library?


Boost is organized by several members of the standard committee.
So it is a breeding ground for libraries that will be in the next standard.

  1. It is an extension to the STL (it fills in the bits left out)
  2. It is well documented.
  3. It is well peer-reviewed.
  4. It has high activity so bugs are found and fixed quickly.
  5. It is platform neutral and works everywhere.
  6. It is free to use.

With tr1 coming up soon it is nice to know that boost already has a lot of the ground covered. A lot of the libraries in tr1 are basically adapted directly from boost originals and thus have been tried and tested. The difference is that they have been moved into the std::tr1 namespace (rather than boost).

All that you need to do is add the following to your compilers default include search path:

<boost-install-path>/boost/tr1/tr1

Then when you include the standard headers boost will automatically import all the required stuff into the namespace std::tr1

For Example:

To use std::tr1::share_ptr you just need to include <memory>. This will give you all the smart pointers with one file.


You can simply read the Boost Background Information page to get a quick overview of why you should use Boost and what you can use it for. Worth the few minutes it takes.


99% portable.

I would say that it has quite a few libraries that are really useful once you discover a need that is solved by boost. Either you code it yourself or you use a very solid library. Having off the shelve source for stuff like Multi-Index, Lambda, Program Options, RegEx, SmartPtr and Tuple is amazing...

The best thing is to spend some time going through the documentation for the different libraries and evaluating whether it could be of any use to you.

Worthy!!


Boost is great, but just playing Devil's Advocate here are some reasons why you may not want to use Boost:

  • Does sometimes fails to compile/work properly on old compilers.
  • It often increases compile times more than less template-heavy approaches.
  • Some Boost code may not do what you think that it does. Read the documentation!
  • Template abuse can lead to unreadable error messages.
  • Template abuse can lead to code hard to step through in the debugger.
  • It is bleeding edge C++. The next version of Boost may no longer compile on your current (older) compiler.

All of this does not mean that you should not have a look at the Boost code and get some ideas yourself even if you do not use Boost as it is.


You get a lot of the things that are coming in C++0x. But aside from that generality, some of the better specifics are a simple regex library, a casting library for casting from strings to ints (Lexical cast):

int iResult = 0;
try
{
    iResult = lexical_cast<int>("4");
}
catch(bad_lexical_cast &)
{
    cout << "Unable to cast string to int";
}

A date/time library, among others...

using namespace boost::gregorian;
date weekstart(2002,Feb,1);
date thursday_next = next_weekday(weekstart, Thursday); // following Thursday

There's also a Python interface (Boost Python), a lexer/parser DSL (Boost Spirit):

// A grammar in C++ for equations
group       = '(' >> expression >> ')';
factor      = integer | group;
term        = factor >> *(('*' >> factor) | ('/' >> factor));
expression  = term >> *(('+' >> term) | ('-' >> term));

and that's just scratching the surface...