Benefits and portability of Boost Library

C++Boost

C++ Problem Overview


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?

C++ Solutions


Solution 1 - C++

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.

Solution 2 - C++

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.

Solution 3 - C++

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

Solution 4 - C++

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.

Solution 5 - C++

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

Solution 6 - C++

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.

Solution 7 - C++

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.

Solution 8 - C++

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.

Solution 9 - C++

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.

Solution 10 - C++

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

Solution 11 - C++

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

Solution 12 - C++

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.

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionMatt PascoeView Question on Stackoverflow
Solution 1 - C++Martin YorkView Answer on Stackoverflow
Solution 2 - C++Mihai LimbășanView Answer on Stackoverflow
Solution 3 - C++argatxaView Answer on Stackoverflow
Solution 4 - C++Jeroen DirksView Answer on Stackoverflow
Solution 5 - C++Douglas MayleView Answer on Stackoverflow
Solution 6 - C++Brian R. BondyView Answer on Stackoverflow
Solution 7 - C++lukeView Answer on Stackoverflow
Solution 8 - C++Martin BeckettView Answer on Stackoverflow
Solution 9 - C++MichelView Answer on Stackoverflow
Solution 10 - C++workmad3View Answer on Stackoverflow
Solution 11 - C++FerruccioView Answer on Stackoverflow
Solution 12 - C++View Answer on Stackoverflow