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 the documentation for an old version of boost. Click here for the latest Boost documentation.

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

Expression semantics

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

See also

Metafunctions, Metafunction Class


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