Examples of practical usage of Boost::MPL?

Andriy Tylychko picture Andriy Tylychko · Jan 9, 2011 · Viewed 13.7k times · Source

Can you share any real-world examples of Boost::MPL usage (except lambdas), just to let me better understand its purposes and field of practical usage? The MPL documentation tutorial has a dimensional analysis example, but maybe because it's such an academic example it hasn't given me a feeling of Boost::MPL and when it can be effectively used.

Answer

jalf picture jalf · Jan 9, 2011

I've used Boost.Mpl to generate variant-like classes.

For example, given a MPL type list such as this:

typedef boost::mpl::set<Foo, Bar, Baz> type_set;

I then use boost::mpl::fold to build a chain of classes derived from each others which each adds an std::unordered_set of one of the types in the type set. The end result is a class which contains an unordered_set<Foo>, an unordered_set<Bar> and an unordered_set<Baz>.

And because the class is specified in terms of a boost::mpl::set, I can iterate over these types to automatically generate other functions as well, such as an operator== which compares all of the unordered_sets.