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 a collection of C++ libraries. 10 of which are being included in tr1 of C++0x.
You can get started with boost here.
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...
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).
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.
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.
- It is an extension to the STL (it fills in the bits left out)
- It is well documented.
- It is well peer-reviewed.
- It has highly activity so bugs are found and fixed quickly.
- It is platform neutral and works everywhere.
- 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.
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.
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!!
All of the above, plus it encourages a lot of modern, best-practice C++ techniques. It tends to improve the quality of your code.
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.
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.