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 to view this page for the latest version.
Front Page / Metafunctions / Miscellaneous / inherit

inherit

Synopsis

template<
      typename T1, typename T2
    >
struct inherit2
{
    typedef unspecified type;
};

...

template<
      typename T1, typename T2,... typename Tn
    >
struct inheritn
{
    typedef unspecified type;
};

template<
      typename T1
    , typename T2
    ...
    , typename Tn = unspecified
    >
struct inherit
{
    typedef unspecified type;
};

Description

Returns an unspecified class type publically derived from T1, T2,... Tn. Guarantees that derivation from empty_base is always a no-op, regardless of the position and number of empty_base classes in T1, T2,... Tn.

Header

#include <boost/mpl/inherit.hpp>

Model of

Metafunction

Parameters

Parameter Requirement Description
T1, T2,... Tn A class type Classes to derived from.

Expression semantics

For artibrary class types t1,t2,... tn:

typedef inherit2<t1,t2>::type r;
Return type:

A class type.

Precondition:

t1 and t2 are complete types.

Semantics:

If both t1 and t2 are identical to empty_base, equivalent to

typedef empty_base r;

otherwise, if t1 is identical to empty_base, equivalent to

typedef t2 r;

otherwise, if t2 is identical to empty_base, equivalent to

typedef t1 r;

otherwise equivalent to

struct r : t1, t2 {};
typedef inheritn<t1,t2,...tn>::type r;
Return type:

A class type.

Precondition:

t1,t2,... tn are complete types.

Semantics:

Equivalent to

struct r
    : inherit2<
          inheritn-1<t1,t2,...tn-1>::type
        , tn
        >
{
};
typedef inherit<t1,t2,...tn>::type r;
Precondition:

t1,t2,... tn are complete types.

Return type:

A class type.

Semantics:

Equivalent to

typedef inheritn<t1,t2,...tn>::type r;

Complexity

Amortized constant time.

Example

struct udt1 { int n; };
struct udt2 {};

typedef inherit<udt1,udt2>::type r1;
typedef inherit<empty_base,udt1>::type r2;
typedef inherit<empty_base,udt1,empty_base,empty_base>::type r3;
typedef inherit<udt1,empty_base,udt2>::type r4;
typedef inherit<empty_base,empty_base>::type r5;

BOOST_MPL_ASSERT(( is_base_and_derived< udt1, r1> ));
BOOST_MPL_ASSERT(( is_base_and_derived< udt2, r1> ));
BOOST_MPL_ASSERT(( is_same< r2, udt1> ));
BOOST_MPL_ASSERT(( is_same< r3, udt1 > ));
BOOST_MPL_ASSERT(( is_base_and_derived< udt1, r4 > ));
BOOST_MPL_ASSERT(( is_base_and_derived< udt2, r4 > ));
BOOST_MPL_ASSERT(( is_same< r5, empty_base > ));

See also

Metafunctions, empty_base, inherit_linearly, identity