Front Page / Tutorial: Metafunctions and Higher-Order Metaprogramming / Handling Placeholders

Handling Placeholders

Our implementation of twice already works with metafunction classes. Ideally, we would like it to work with placeholder expressions too, much the same as mpl::transform allows us to pass either form. For example, we would like to be able to write:

template <class X>
struct two_pointers
    : twice<boost::add_pointer<_1>, X>
{};

But when we look at the implementation of boost::add_pointer, it becomes clear that the current definition of twice can't work that way.

template <class T>
struct add_pointer
{
    typedef T* type;
};

To be invokable by twice, boost::add_pointer<_1> would have to be a metafunction class, along the lines of add_pointer_f. Instead, it's just a nullary metafunction returning the almost senseless type _1*. Any attempt to use two_pointers will fail when apply1 reaches for a nested ::apply metafunction in boost::add_pointer<_1> and finds that it doesn't exist.

We've determined that we don't get the behavior we want automatically, so what next? Since mpl::transform can do this sort of thing, there ought to be a way for us to do it too — and so there is.