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

Front Page / Metafunctions / Composition and Argument Binding / bind

bind

Synopsis

template<
      typename F
    >
struct bind0
{
    // unspecified
    // ...
};

template<
      typename F, typename A1
    >
struct bind1
{
    // unspecified
    // ...
};

...

template<
      typename F, typename A1,... typename An
    >
struct bindn
{
    // unspecified
    // ...
};

template<
      typename F
    , typename A1 = unspecified
    ...
    , typename An = unspecified
    >
struct bind
{
    // unspecified
    // ...
};

Description

bind is a higher-order primitive for Metafunction Class composition and argument binding. In essence, it's a compile-time counterpart of the similar run-time functionality provided by Boost.Bind and Boost.Lambda libraries.

Header

#include <boost/mpl/bind.hpp>

Model of

Metafunction Class

Parameters

Parameter Requirement Description
F Metafunction Class An metafunction class to perform binding on.
A1,... An Any type Arguments to bind.

Expression semantics

For any Metafunction Class f and arbitrary types a1,... an:

typedef bind<f,a1,...an> g;
typedef bindn<f,a1,...an> g;
Return type:Metafunction Class
Semantics:

Equivalent to

struct g
{
    template<
          typename U1 = unspecified
        ...
        , typename Un = unspecified
        >
    struct apply
        : apply_wrapn<
              typename h0<f,U1,...Un>::type
            , typename h1<a1,U1,...Un>::type
            ...
            , typename hn<an,U1,...Un>::type
            >
    {
    };
};

where hk is equivalent to

template< typename X, typename U1,... typename Un > struct hk
    : apply_wrapn<X,U1,...Un>
{
};

if f or ak is a bind expression or a placeholder, and

template< typename X, typename U1,... typename Un > struct hk
{
    typedef X type;
};

otherwise. [Note: Every nth appearance of the unnamed placeholder in the bind<f,a1,...an> specialization is replaced with the corresponding numbered placeholder _nend note]

Example

struct f1
{
    template< typename T1 > struct apply
    {
        typedef T1 type;
    };
};

struct f5
{
    template< typename T1, typename T2, typename T3, typename T4, typename T5 >
    struct apply
    {
        typedef T5 type;
    };
};

typedef apply_wrap1<
      bind1<f1,_1>
    , int
    >::type r11;

typedef apply_wrap5<
      bind1<f1,_5>
    , void,void,void,void,int
    >::type r12;

BOOST_MPL_ASSERT(( is_same<r11,int> ));
BOOST_MPL_ASSERT(( is_same<r12,int> ));

typedef apply_wrap5<
      bind5<f5,_1,_2,_3,_4,_5>
    , void,void,void,void,int
    >::type r51;

typedef apply_wrap5<
      bind5<f5,_5,_4,_3,_2,_1>
    , int,void,void,void,void
    >::type r52;

BOOST_MPL_ASSERT(( is_same<r51,int> ));
BOOST_MPL_ASSERT(( is_same<r52,int> ));

See also

Composition and Argument Binding, invocation, Placeholders, lambda, quote, protect, apply, apply_wrap