boost::mpl::Metafunction

# Metafunction

### Description

A metafunction is a class or a class template that represents a function invocable at compile-time. A non-nullary metafunction is invoked by instantiating the class template with particular template parameters (metafunction arguments); the result of the metafunction application is accessible through the instantiation's nested `type` typedef. All metafunction's arguments must be types (i.e. only type template parameters are allowed). A metafunction can have a variable number of parameters. A nullary metafunction is represented as a class with a nested `type` typename member.

### Valid expressions

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

### Expression semantics

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

### Example

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

    // unary metafunction
    template< typename T > struct sizeof_
    {
        typedef int_< sizeof(T) > type;
    };

    // binary metafunction
    template< typename T1, typename T2 >
    struct is_same
    {
        typedef false_ type;
    };

    template< typename T >
    struct is_same<T,T>
    {
        typedef true_ type;
    };

    // invocations
    typedef always_true::type t1;
    typedef sizeof_<int>::type t2;
    typedef is_same<int,char>::type t3;

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

### Models

- `plus`
- `not_`
- `size`
- `max_element`

### See also

Metafunctions, Metafunction Class

Table of Contents
Last edited March 10, 2003 3:27 am