Home > Articles > Programming > C/C++

  • Print
  • + Share This
This chapter is from the book

3.5 Lambda Details

Now that you have an idea of the semantics of MPL's lambda facility, let's formalize our understanding and look at things a little more deeply.

3.5.1 Placeholders

The definition of "placeholder" may surprise you:

3.5.1.1 Implementation

The convenient names _1, _2,... _5 are actually typedefs for specializations of mpl::arg that simply select the Nth argument for any N. [6] The implementation of placeholders looks something like this:

    namespace boost { namespace mpl { namespace placeholders {

    template <int N> struct arg; // forward declarations
    struct void_;

    template <>
    struct arg<1>
    {
        template <
          class A1, class A2 = void_, ... class Am = void_>
        struct apply
        {
            typedef A1 type; // return the first argument
        };
    };
    typedef arg<1> _1;

    template <>
    struct arg<2>
    {
        template <
          class A1, class A2, class A3 = void_, ...class Am = void_
        >
        struct apply
        {
            typedef A2 type; // return the second argument
        };
    };
    typedef arg<2> _2;

    
   more specializations and typedefs...

    }}}

Remember that invoking a metafunction class is the same as invoking its nested apply metafunction. When a placeholder in a lambda expression is evaluated, it is invoked on the expression's actual arguments, returning just one of them. The results are then substituted back into the lambda expression and the evaluation process continues.

3.5.1.2 The Unnamed Placeholder

There's one special placeholder, known as the unnamed placeholder, that we haven't yet defined:

    namespace boost { namespace mpl { namespace placeholders {

    typedef arg<-1> _; // the unnamed placeholder

    }}}

The details of its implementation aren't important; all you really need to know about the unnamed placeholder is that it gets special treatment. When a lambda expression is being transformed into a metafunction class by mpl::lambda,

the nth appearance of the unnamed placeholder in a given template specialization is replaced with _n.

So, for example, every row of Table 3.1 contains two equivalent lambda expressions.

Table 3.1. Unnamed Placeholder Semantics

mpl::plus<_,_>

mpl::plus<_1,_2>

boost::is_same<
    _
  , boost::add_pointer<_>
>

boost::is_same<
    _1
  , boost::add_pointer<_1>
>

mpl::multiplies<
   mpl::plus<_,_>
 , mpl::minus<_,_>
>

mpl::multiplies<
   mpl::plus<_1,_2>
 , mpl::minus<_1,_2>
>

Especially when used in simple lambda expressions, the unnamed placeholder often eliminates just enough syntactic "noise" to significantly improve readability.

3.5.2 Placeholder Expression Definition

Now that you know just what placeholder means, we can define placeholder expression:

In other words, a placeholder expression always involves a placeholder.

3.5.3 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 of lambda 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;

3.5.4 The Importance of Being Lazy

Recall the definition of always_int from the previous chapter:

    struct always_int
    {
        typedef int type;
    };

Nullary metafunctions might not seem very important at first, since something like add_pointer<int> could be replaced by int* in any lambda expression where it appears. Not all nullary metafunctions are that simple, though:

    struct add_pointer_f
    {
        template <class T>
        struct apply : boost::add_pointer<T> {};
    };
    typedef mpl::vector<int, char*, double&> seq;
    typedef mpl::transform<seq, boost::add_pointer<_> > calc_ptr_seq;

Note that calc_ptr_seq is a nullary metafunction, since it has transform's nested ::type. A C++ template is not instantiated until we actually "look inside it," though. Just naming calc_ptr_seq does not cause it to be evaluated, since we haven't accessed its ::type yet.

Metafunctions can be invoked lazily, rather than immediately upon supplying all of their arguments. We can use lazy evaluation to improve compilation time when a metafunction result is only going to be used conditionally. We can sometimes also avoid contorting program structure by naming an invalid computation without actually performing it. That's what we've done with calc_ptr_seq above, since you can't legally form double&*. Laziness and all of its virtues will be a recurring theme throughout this book.

  • + Share This
  • 🔖 Save To Your Account