Boost C++ Libraries

...one of the most highly regarded and expertly designed C++ library projects in the world. Herb Sutter and Andrei Alexandrescu, C++ Coding Standards

Front Page / Tutorial: Metafunctions and Higher-Order Metaprogramming / Lambda Details / Lambda and Non-Metafunction Templates

Lambda and Non-Metafunction Templates

There is just one detail of placeholder expressions that we haven't discussed yet. MPL uses a special rule to make it easier to integrate ordinary templates into metaprograms: After all of the placeholders have been replaced with actual arguments, if the resulting template specialization X doesn't have a nested ::type, the result is just X itself.

For example, mpl::apply<std::vector<_>, T> is always just std::vector<T>. If it weren't for this behavior, we would have to build trivial metafunctions to create ordinary template specializations in lambda expressions:

// trivial std::vector generator
template<class U> 
struct make_vector { typedef std::vector<U> type; };

typedef mpl::apply<make_vector<_>, T>::type vector_of_t;

Instead, we can simply write:

typedef mpl::apply<std::vector<_>, T>::type vector_of_t;