• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1.. Sequences/Intrinsic Metafunctions//size
2
3size
4====
5
6Synopsis
7--------
8
9.. parsed-literal::
10
11    template<
12          typename Sequence
13        >
14    struct size
15    {
16        typedef |unspecified| type;
17    };
18
19
20
21Description
22-----------
23
24``size`` returns the number of elements in the sequence, that is, the number of elements
25in the range [``begin<Sequence>::type``, ``end<Sequence>::type``).
26
27
28Header
29------
30
31.. parsed-literal::
32
33    #include <boost/mpl/size.hpp>
34
35
36
37Model of
38--------
39
40|Tag Dispatched Metafunction|
41
42
43Parameters
44----------
45
46+---------------+-----------------------+-----------------------------------------------+
47| Parameter     | Requirement           | Description                                   |
48+===============+=======================+===============================================+
49| ``Sequence``  | |Forward Sequence|    | A sequence to query.                          |
50+---------------+-----------------------+-----------------------------------------------+
51
52
53Expression semantics
54--------------------
55
56
57For any |Forward Sequence| ``s``:
58
59
60.. parsed-literal::
61
62    typedef size<s>::type n;
63
64:Return type:
65    |Integral Constant|.
66
67:Semantics:
68    Equivalent to
69
70    .. parsed-literal::
71
72       typedef distance< begin<s>::type,end<s>::type >::type n;
73
74
75:Postcondition:
76    ``n::value >= 0``.
77
78
79
80Complexity
81----------
82
83The complexity of the ``size`` metafunction directly depends on the implementation of
84the particular sequence it is applied to. In the worst case, ``size`` guarantees a
85linear complexity.
86
87If the ``s`` is a |Random Access Sequence|, ``size<s>::type`` is an |O(1)| operation.
88The opposite is not necessarily true |--| for example, a sequence class that models
89|Forward Sequence| might still give us an |O(1)| ``size`` implementation.
90
91
92Example
93-------
94
95.. parsed-literal::
96
97    typedef list0<> empty_list;
98    typedef vector_c<int,0,1,2,3,4,5> numbers;
99    typedef range_c<int,0,100> more_numbers;
100
101    BOOST_MPL_ASSERT_RELATION( size<list>::value, ==, 0 );
102    BOOST_MPL_ASSERT_RELATION( size<numbers>::value, ==, 5 );
103    BOOST_MPL_ASSERT_RELATION( size<more_numbers>::value, ==, 100 );
104
105
106See also
107--------
108
109|Forward Sequence|, |Random Access Sequence|, |empty|, |begin|, |end|, |distance|
110
111
112.. copyright:: Copyright �  2001-2009 Aleksey Gurtovoy and David Abrahams
113   Distributed under the Boost Software License, Version 1.0. (See accompanying
114   file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
115