boost::mpl::Metafunction Class

# Metafunction Class

### Description

A metafunction class is a certain form of metafunction representation that enables higher-order metaprogramming. In particular, a non-nullary metafunction class is a type with a nested class template member `apply`. A nullary metafunction class has the form of a nullary metafunction. A metafunction class invocation is defined as invocation of its nested `apply` metafunction.

### Valid expressions

Expression | Expression type
--- | ---
`typename f::type` | A type
`typename f::template apply<a1,..,an>::type` | A type

### Expression semantics

Expression | Complexity | Precondition | Semantics | Postcondition
--- | --- | --- | --- | ---
`typename f::type` | unspecified | `f` is a nullary metafunction class; `f::type` is a type-name | `f::type` is the result of the metafunction class invocation |
`typename f::template apply<a1,..,an>::type` | unspecified | `f` is an `n`-ary metafunction class; `apply` is a metafunction | `typename f::template apply<a1,..,an>::type` is the result of the metafunction class invocation with the actual arguments `a1,..,an` |

### Example

    // nullary metafunction class
    struct always_true { typedef true_ type; };

    template< long N > struct le
    {
        template< typename M > struct apply
        {
            typedef bool_< (M::value < N) > type;
        };
    };

    // unary metafunction class
    typedef le<5> less_than_5;

    // binary metafunction class
    struct less_than
    {
        template< typename N1, typename N2 > struct apply
        {
            typedef bool_< (N1::value < N2::value) > type;
        };
    };

    // invocations
    typedef always_true::type t1;
    typedef less_than_5::apply< int_<7> >::type t2;
    typedef less_than::apply< int_<5>,int_<7> >::type t3;

    // results checks
    BOOST_STATIC_ASSERT(t1::value);
    BOOST_STATIC_ASSERT(!t2::value);
    BOOST_STATIC_ASSERT(t3::value);

### See also

Metafunctions, Metafunction

Table of Contents
Last edited March 10, 2003 4:16 am