]>
Commit | Line | Data |
---|---|---|
1 | .. Sequences/Intrinsic Metafunctions//size | |
2 | ||
3 | size | |
4 | ==== | |
5 | ||
6 | Synopsis | |
7 | -------- | |
8 | ||
9 | .. parsed-literal:: | |
10 | ||
11 | template< | |
12 | typename Sequence | |
13 | > | |
14 | struct size | |
15 | { | |
16 | typedef |unspecified| type; | |
17 | }; | |
18 | ||
19 | ||
20 | ||
21 | Description | |
22 | ----------- | |
23 | ||
24 | ``size`` returns the number of elements in the sequence, that is, the number of elements | |
25 | in the range [``begin<Sequence>::type``, ``end<Sequence>::type``). | |
26 | ||
27 | ||
28 | Header | |
29 | ------ | |
30 | ||
31 | .. parsed-literal:: | |
32 | ||
33 | #include <boost/mpl/size.hpp> | |
34 | ||
35 | ||
36 | ||
37 | Model of | |
38 | -------- | |
39 | ||
40 | |Tag Dispatched Metafunction| | |
41 | ||
42 | ||
43 | Parameters | |
44 | ---------- | |
45 | ||
46 | +---------------+-----------------------+-----------------------------------------------+ | |
47 | | Parameter | Requirement | Description | | |
48 | +===============+=======================+===============================================+ | |
49 | | ``Sequence`` | |Forward Sequence| | A sequence to query. | | |
50 | +---------------+-----------------------+-----------------------------------------------+ | |
51 | ||
52 | ||
53 | Expression semantics | |
54 | -------------------- | |
55 | ||
56 | ||
57 | For 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 | ||
80 | Complexity | |
81 | ---------- | |
82 | ||
83 | The complexity of the ``size`` metafunction directly depends on the implementation of | |
84 | the particular sequence it is applied to. In the worst case, ``size`` guarantees a | |
85 | linear complexity. | |
86 | ||
87 | If the ``s`` is a |Random Access Sequence|, ``size<s>::type`` is an |O(1)| operation. | |
88 | The 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 | ||
92 | Example | |
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 | ||
106 | See also | |
107 | -------- | |
108 | ||
109 | |Forward Sequence|, |Random Access Sequence|, |empty|, |begin|, |end|, |distance| | |
110 | ||
111 | ||
112 |