Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Examples of practical usage of Boost::MPL?

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.

like image 519
Andriy Tylychko Avatar asked Jan 09 '11 11:01

Andriy Tylychko


2 Answers

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.

like image 144
jalf Avatar answered Sep 27 '22 20:09

jalf


The fact is, Boost.MPL, like Boost.Preprocessor, are really building blocks.

Most of the times, you probably use it through other libraries, as a number of Boost libraries are built upon those two.

For example:

  • Boost.Fusion (which crosses the gaps between compile-time and run-time realms)
  • Boost.MultiIndex (for an easier interface)
  • Boost.Unit (for dimensional analysis)
  • Boost.Variant may, I think, also depends on it

You may use it unknowningly already :)

like image 45
Matthieu M. Avatar answered Sep 27 '22 20:09

Matthieu M.