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

This is an old version of boost. Click here for the latest version's documentation home page.

[Home]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::typeA type
typename f::template apply<a1,..,an>::typeA type

Expression semantics

 Expression  Complexity  Precondition  Semantics  Postcondition 
typename f::typeunspecifiedf is a nullary metafunction class; f::type is a type-namef::type is the result of the metafunction class invocation
typename f::template apply<a1,..,an>::typeunspecifiedf is an n-ary metafunction class; apply is a metafunctiontypename 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