tags:

views:

1842

answers:

12
+22  Q: 

Boost Library

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?

+5  A: 

Boost is a collection of C++ libraries. 10 of which are being included in tr1 of C++0x.

You can get started with boost here.

Brian R. Bondy
+7  A: 

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 case 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...

Douglas Mayle
+2  A: 

Boost is a very extensive library of (usually) generic constructs that can help in almost any application. This can be shown by the fact that a lot of boost components have been included in the C++ 0x specifications.

It is also portable across at least the major platforms, and should be portable to almost anything with a mostly standards compliant C++ compiler.

The only warning is that there can be a lot of mingled dependencies between boost libraries, making it harder to pick out just a specific component to distribute (other than the entire boost library).

workmad3
Just as extension to this post: To extract subsets of Boost you can use the bcp utility http://www.boost.org/doc/libs/1_36_0/tools/bcp/bcp.html
jk
Actually there are relatively few intra-dependencies. This is a specific design goal of boost. http://www.boost.org/development/reuse.html
Greg Rogers
The bulk of Boost is implemented as header-only. For the remaining stuff, you are probably better off linking to it statically rather than distributing DLLs/shared libs.
Ferruccio
+12  A: 

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.

Mihai Limbășan
I'm astonished the author of this question didn't think to read this.
PP
In his defense, now it is available here and searchable for everyone on the site (and probably high in google search results).
Justin Ethier
+43  A: 

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 highly 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.

Martin York
+1  A: 

Boost is a collection of high quality peer reviewed C++ libraries that place emphasis on portability and correctness. It acts as the defacto proving grounds for new additions to the language and the standard library. Check out their website for more details.

luke
+3  A: 

Boost's advantages: It's widely available, will port to any modern C++ compiler or about any platform.
The functions are platform independant, you don't have to learn a new thread design for each new framework.
It encapsulates a lot of platfom specific functions, like filesystems in a standard way.

It's what C++ should have shipped with! A lot of Java's popularity was that is shipped with a standard library to do prety much everything you wanted. C++ unfortunately only inherited the limited C/Unix standard functions.

Martin Beckett
+9  A: 

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!!

argatxa
Several parts of Boost failed on the C++ compilers from Sun and IBM just a couple years back. Not sure about the situation now, but I wouldn't rate the portability anywhere near 99%.
John Zwinck
+1  A: 

All of the above, plus it encourages a lot of modern, best-practice C++ techniques. It tends to improve the quality of your code.

Ferruccio
+6  A: 

Boost is great but just playing Devils 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 message.
  • 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 you 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.

James Dean
With bullet 5: Agreed! Try debugging the boost graph library...
Raindog
+1  A: 

shared_ptr and weak_ptr, especially in multithreaded code, are alone worth installing boost. BOOST_STATIC_ASSERT is also pretty cool for doing compile-time logic checking.

The fact that a lot of the classes and utilities in boost are in headers, meaning you can get a lot of functionality without having to compile anything at all, is also a plus. Portability usually isn't a problem, unless you use an extremely old compiler. I once tried to get MPL to work with VC6 and it printed out 40,000 warnings/internal errors before exploding completely. But in general most of the library should work regardless of your platform or compiler vendor.

Take into consideration the fact that quite a few things from Boost are already in TR1, and will most likely be in the next revision of the C++ standard library. That's a pretty big endorsement.

Michel
A: 

Also note most of boost is templates so does not require building
(just include the correct header files).

The few parts that do require building are optional:
These can each be built independently thus preventing unnecessary bloat for unneeded code.