Most used parts of Boost

Viewed 20075

When I discovered boost::lexical_cast I thought to myself "why didn't I know about this sooner!" - I hated having to write code like

stringstream ss;
ss << anIntVal;
mystring = ss.str();

Now I write

mystring = boost::lexical_cast<string>(anIntVal);

Yesterday, on stackoverflow, I came across boost split (another gem that will save me writing code).

string stringtobesplit = "AA/BB-CC")
vector<string> tokens;

boost::split(tokens, stringtobesplit, boost::is_any_of("/-")); 
// tokens now holds 3 items: AA BB CC

I am going to start looking through boost documentation looking for other functions that I will be able to use regularly, but I feel that it will be very easy to miss things.

What boost functions do you use most / would hate not to have?

25 Answers

BOOST_FOREACH makes life worthwhile again.

(Why has nobody mentioned this? The question was asked 8 months ago!)

My faves are, in no particular order:

  • regex
  • filesystem
  • thread
  • lexical_cast
  • program_options (just brilliant!)
  • test (for all my unit testing needs).
  • String algorithms
  • String tokenizer
  • format (type-safe printf style string formatting)
  • smart ptrs

Boost was a massive help when I wrote my first cross-platform app - without it I really would have struggled.

I like how you can supply your own destructor for shared_ptr.
This means, for example, you can use it with FILE* and get it to close the file for you.
eg

void safeclose(FILE*fp) {
    if(fp) {
        fclose(fp);
    }
}
void some_fn() {
    boost::shared_ptr<FILE> fp( fopen(myfilename, "a+t"), safeclose );
    //body of the function, and when ever it exits the file gets closed
    fprintf( fp.get(), "a message\n" );
}

Nobody has mentioned Multi-Index Containers so I'll chime in late. It's not that often that you need them, but without boost it is a real pain to create an equivalent data structure, as well as being less efficient. I've been using them a lot recently to create containers that have look up on 2 keys.

I'm surprised that no one has mentioned boost::optional. I find myself using it more often than any part of Boost except shared_ptr and scoped_ptr.

Nobody mentions boost::tuple? For shame!

One of my most used is not in Boost proper, but the Adobe Source Libraries (ASL) built on top of Boost — specifically, the extensions to the standard algorithms that accept a boost::range in place of separate begin/end iterators. Then instead of calling, say,

std::for_each(some_container.begin(), some_container.end(), do_something());

I can simply say

adobe::for_each(some_container, do_something());

(I do hope these parts of ASL migrate to Boost eventually.)

I use a lot:

  • boost::signals
  • boost::shared_ptr
  • boost::lexical_cast
  • boost::bind
  • boost::random
  • boost::thread
  • boost::noncopyable

Other like Tuple, Static Assert and Integer are very useful if you are writing a library which is due to be used on a variety of platforms.

Things like Graphs and Lambda are more specific.

boost::shared_ptr is a requirement for modern C++ programming IMHO. That's why they added it to the standard with TR1. boost::program_options, boost::bind, and boost::signal are really nice if you know what they are for and how to use them. The last two tend to scare newcomers though.

I've been using shared_ptr for years now. It's just so useful, there's no reason that a project should be without it.

On top of that, I also use Bind/Function/Lambda for generic callback mechanisms -- especially useful when testing -- as well as Format for my general-purpose sprintf replacement.

Finally, it was just the other day when I used Variant in anger to solve a problem (a parser that could respond with a small, fixed set of unrelated token types). The solution was very elegant, and I'm very happy with it.


Years have passed and times have changed, so time for an update. SharedPtr and Function are now part of the Standard, and Bind and Lambda are obsoleted by actual language-level lambda functionality.

I still use Variant (which has also been standardized, but I'm not there yet), Format is largely replaced by fmtlib (which has also been standardized).

The big part of Boost that I use is Boost.Asio. Which is in the process of being standardized.

We found boost::spirit pretty useful for a business solution to parse ECMAScript. Complex, but very nice!

You should check boost::program_options. It makes command line parsing much easier.

What I use the most is now available in the TR1:

  • shared pointers
  • array class

Now I also use pool classes and some other more specific things.

You understand now that Boost is meant to be useful to most programmers, that's why it's the test bed for the future standard library.

I love boost::random and boost::asio and boost::filesystem, however boost::bind , boost::circular_buffer and boost::thread are very practical, smart pointers are ok but I prefer RAII instead as memory management

Okay, here is a new one I've found:
Instead of using stricmp I can use boost's equals function and pass in the is_iequal predicate
eg:
instead of

stricmp( "avalue", mystr.c_str() ) == 0

I can use

equals( "avalue", mystr, is_iequal() ) 

given:

#include <boost/algorithm/string.hpp>
using namespace boost::algorithm;

Talking about boost::lexical_cast, why isn't something like 'format' a static member in the std::string library?
Almost all gui libs have something like CString::Format("%i") or QString::Number("%i") which return an initialised string.

Related