Benefits and Portability of Boost Library

Benefits and portability of 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.

What are the advantages of using the C++ Boost libraries?

Boost is used so extensively because:

  • It is open-source and peer-reviewed.
  • It provides a wide range of platform agnostic functionality that STL missed.
  • It is a complement to STL rather than a replacement.
  • Many of Boost developers are on the C++ standard committee. In fact, many parts of Boost is considered to be included in the next C++ standard library.
  • It is documented nicely.
  • Its license allows inclusion in open-source and closed-source projects.
  • Its features are not usually dependent on each other so you can link only the parts you require. [Luc Hermitte's comment]

Use boost C++ libraries?

What exactly is Boost?

Boost is a collection of useful and extremely high-quality libraries for C++ that complement the rather small standard library.

What are the most import reasons to use Boost?

Boost offers high-quality tools that are missing from C++. Their use is extremely varied though so whether Boost is for you depends entirely on your needs. But I can safely say that every large enough C++ code base would benefit from using Boost.

Some of the most versatile parts are the shared_ptr (a reference-counting smart pointer that helps prevent memory leaks in pointer-rich code), array which provides a very convenient wrapper around C-style arrays of fixed size and other small odd bits which have been integrated into the next C++ standard.

Is it fully cross-platform?

Almost always yes. This is one of the main qualities of Boost.

Is there any link to a page describing all the modules of Boost in one or two sentences?

There is indeed.

What are the Best Components of Boost?

I use quite frequently (and it makes my life simpler):

  • smart pointers (shared_ptr, scoped_ptr, weak_ptr, interprocess unique_ptr):

    • scoped_ptr for basic RAII (without shared ownership and ownership transfer), at no cost.
    • shared_ptr for more complex operations - when shared ownership is needed. However there is some cost.
    • unique_ptr - there is active work at boost on unifying various approaches (present at Boost) to unique_ptr with move emulation.
    • They are really simple to use (header only), easy to learn and very well tested (well, except maybe the unique_ptr)
  • Boost Thread - actively developed (threads are now movable) library for working with threads. Hides the complexity of thread implementation on a given platform.

  • Boost MPL and Fusion - these are more difficult to explain. For long time I didn't use compile time power, but after some reading and learning it turned out that some of my code can be nicely simplified. Still, beware of the compilation time...

  • Boost Asio

    • Contrary to the first impression (at least some time ago) it is not only the networking library. It provides asynchronous I/O model that can be used for virtually anything.
  • Boost Format (powerful output formatting, but very heavy)

  • Boost Spirit2x (Karma and Qi used both for parsing and generating output based on a given grammar). Really powerful, can create a parser without resorting to external tools. Yet the compilation time might be a problem. Also version 2x is being actively developed and the documentation is rather scarce (the spirit-devel mailing list is very helpful though)

  • Boost Bind, Function and Lambda to make your life easier and Boost Phoenix - just to experiment

  • lexical_cast (something similar might be born soon as boost::string)

  • Regex/Xpressive - regular expressions

  • Type traits and concept checks - once again to make your life easier

  • Math:

    • various random number generators
    • various statistical distributions
    • ublas - for using LAPACK/BLAS bindings in C++ like way
    • some mathematical functions, normally not available in C++
    • some tools for controlling the conversions between numreric types
    • interval arithmetics
  • Boost Iterator (specialized adaptors for iterators and facade for creating your own)

  • Boost Unit Testing framework

And still there are some parts that I'd barely touched in Boost. Probably I also forgot to mention few obvious ones.

Remember to use right tools (hammers) for right problems (nails). Remember to keep the solutions simple. Remember about the cost of received functionality (for example shared_ptr or boost::format runtime overhead or MPL/Fusion/Spirit/Phoenix compile time costs and executable sizes). But experiment and learn - it's where the fun is.

And when it comes to convincing the management to use the new libraries - you don't have to start with all the libraries. Start with the simple things (probably the ones that have a long and stable Boost history, broad compiler support, are planned for inclusion in TR2/C++1x, etc) and simple examples that show the benefits.

What are the advantages of boost::noncopyable

Summarizing what others have said:

Advantages of boost::noncopyable over private copy methods:

  1. It is more explicit and descriptive in the intent. Using private copy functions is an idiom that takes longer to spot than noncopyable.
  2. It is less code / less typing / less clutter / less room for error (the easiest would be accidentally providing an implementation).
  3. It embeds meaning right in the type's metadata, similar to a C# attribute. You can now write a function which accepts only objects which are noncopyable.
  4. It potentially catches errors earlier in the build process. The error will be presented at compile-time rather than link-time, in the case that the class itself or friends of the class are doing the erroneous copying.
  5. (almost the same as #4) Prevents the class itself or friends of the class from calling the private copy methods.

Advantages of private copy methods over boost::noncopyable:

  1. No boost dependency

Should I prefer std::thread or Boost threads?

If you're not already using boost in your project, there is no reason to use boost::thread in favor of std::thread. That is unless you are using some feature from boost not available in the STL. std::thread is suitable enough for most use cases, and unless compelling arguments are presented, writing standard code is always preferable.

If however you are already using boost in your project, check out if boost::thread offers anything extra compared to std::thread.

how do i get started using boost

Boost has an unimaginable number of libraries.
Easy ones to get started on are

  • noncopyable
  • array
  • circular_buffer
  • foreach
  • operators (one of my personal favorites)
  • smart_ptr
  • date_time

More advanced ones include

  • lambda
  • bind
  • iostreams
  • serialization
  • threads

Getting used to boost takes time, but I assure you it will make your life much better. Plus, looking at how the boost libraries are coded will help you get better at c++ coding, especially templates.

You mentioned what should you look up before trying boost. I agree that function objects are a great thing to research. Also, make sure to look up about template programming. A common problem to make sure you know is when to use the typename qualifier for dependent types. For the most part, however, the libraries are very well documented, with examples and reference materials.

ACE vs Boost vs POCO

As rdbound said, Boost has a "near STL" status. So if you don't need another library, stick to Boost. However, I use POCO because it has some advantages for my situation. The good things about POCO IMO:

  • Better thread library, especially a Active Method implementation. I also like the fact that you can set the thread priority.

  • More comprehensive network library than boost::asio. However boost::asio is also a very good library.

  • Includes functionality that is not in Boost, like XML and database interface to name a few.

  • It is more integrated as one library than Boost.

  • It has clean, modern and understandable C++ code. I find it far easier to understand than most of the Boost libraries (but I am not a template programming expert :)).

  • It can be used on a lot of platforms.

Some disadvantages of POCO are:

  • It has limited documentation. This somewhat offset by the fact that the source is easy to understand.

  • It has a far smaller community and user base than, say, Boost. So if you put a question on Stack Overflow for example, your chances of getting an answer are less than for Boost

  • It remains to be seen how well it will be integrated with the new C++ standard. You know for sure that it will not be a problem for Boost.

I never used ACE, so I can't really comment on it. From what I've heard, people find POCO more modern and easier to use than ACE.

Some answers to the comments by Rahul:

  1. I don't know about versatile and advanced. The POCO thread library provides some functionality that is not in Boost: ActiveMethod and Activity, and ThreadPool. IMO POCO threads are also easier to use and understand, but this is a subjective matter.

  2. POCO network library also provides support for higher level protocols like HTTP and SSL (possibly also in boost::asio, but I am not sure?).

  3. Fair enough.

  4. Integrated library has the advantage of having consistent coding, documentation and general "look and feel".

  5. Being cross-platform is an important feature of POCO, this is not an advantage in relation to Boost.

Again, you should probably only consider POCO if it provides some functionality you need and that is not in Boost.



Related Topics



Leave a reply



Submit