Examples of practical usage of Boost::MPL?
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_set
s.
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 :)
I use a more enhanced dimensional analysis library called Boost.Units.
I've developed a compile-time reflection library and then used that library to build a generic class that provides runtime-reflection to any compile-time reflected type passed in. I've used that support to automatically generate UI components to edit the properties of such reflected types.
It's also paramount to the distribution of events within our application. For instance, when someone changes the units they wish the system to be in, I don't have to teach that system that new items have been added to given devices because the code uses MPL to analyze those types and just knows that something's been added and changes it.
I've just used metaprogramming techniques to wrap up the Qt signals into something that regains the type safety removed by their system and is able to connect with any functional entity.
But to tell the truth, you've almost certainly used practically applied metaprogramming techniques already when you've used standard algorithms like sort. A decent implementation of the sort algorithm uses a less evolved form of metaprogramming to analyze the iterators passed in and then uses tag-dispatching to initiate a sort algorithm capable of fully utilizing the features of those iterators.
Quite frankly, if you're not doing metaprogramming then you're not utilizing the power of C++ and you may as well be using something else.