Boost C++ Libraries 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 / Sequences / Intrinsic Metafunctions / size



      typename Sequence
struct size
    typedef unspecified type;


size returns the number of elements in the sequence, that is, the number of elements in the range [begin<Sequence>::type, end<Sequence>::type).


#include <boost/mpl/size.hpp>

Model of

Tag Dispatched Metafunction


Parameter Requirement Description
Sequence Forward Sequence A sequence to query.

Expression semantics

For any Forward Sequence s:

typedef size<s>::type n;
Return type:

Integral Constant.


Equivalent to

typedef distance< begin<s>::type,end<s>::type >::type n;

n::value >= 0.


The complexity of the size metafunction directly depends on the implementation of the particular sequence it is applied to. In the worst case, size guarantees a linear complexity.

If the s is a Random Access Sequence, size<s>::type is an O(1) operation. The opposite is not necessarily true — for example, a sequence class that models Forward Sequence might still give us an O(1) size implementation.


typedef list0<> empty_list;
typedef vector_c<int,0,1,2,3,4,5> numbers;
typedef range_c<int,0,100> more_numbers;

BOOST_MPL_ASSERT_RELATION( size<list>::value, ==, 0 );
BOOST_MPL_ASSERT_RELATION( size<numbers>::value, ==, 5 );
BOOST_MPL_ASSERT_RELATION( size<more_numbers>::value, ==, 100 );

See also

Forward Sequence, Random Access Sequence, empty, begin, end, distance