3 // Copyright Thorsten Ottosen 2003-2008. Use, modification and
4 // distribution is subject to the Boost Software License, Version
5 // 1.0. (See accompanying file LICENSE_1_0.txt or copy at
6 // http://www.boost.org/LICENSE_1_0.txt)
8 // For more information, see http://www.boost.org/libs/range/
11 #include <boost/range.hpp>
12 #include <iterator> // for std::iterator_traits, std::distance()
17 // Our sample UDT. A 'Pair'
18 // will work as a range when the stored
19 // elements are iterators.
32 // Specialize metafunctions. We must include the range.hpp header.
33 // We must open the 'boost' namespace.
37 struct range_value< Foo::Pair<T> >
39 typedef typename std::iterator_traits<T>::value_type type;
44 struct range_iterator
< Foo::Pair
<T
> >
50 struct range_const_iterator
< Foo::Pair
<T
> >
53 // Remark: this is defined similar to 'range_iterator'
54 // because the 'Pair' type does not distinguish
55 // between an iterator and a const_iterator.
62 struct range_difference< Foo::Pair<T> >
64 typedef typename std::iterator_traits<T>::difference_type type;
69 struct range_size
< Foo::Pair
<T
> >
71 int static_assertion
[ sizeof( std::size_t ) >=
72 sizeof( typename range_difference
< Foo::Pair
<T
> >::type
) ];
73 typedef std::size_t type
;
76 } // namespace 'boost'
81 // The required functions. These should be defined in
82 // the same namespace as 'Pair', in this case
83 // in namespace 'Foo'.
87 inline T
boost_range_begin( Pair
<T
>& x
)
93 inline T
boost_range_begin( const Pair
<T
>& x
)
99 inline T
boost_range_end( Pair
<T
>& x
)
105 inline T
boost_range_end( const Pair
<T
>& x
)
111 inline typename
boost::range_size
< Pair
<T
> >::type
112 boost_range_size( const Pair
<T
>& x
)
114 return std::distance(x
.first
,x
.last
);
123 typedef std::vector
<int>::iterator iter
;
124 std::vector
<int> vec
;
126 Foo::Pair
<iter
> pair
= { vec
.begin(), vec
.end() };
127 const Foo::Pair
<iter
>& cpair
= pair
;
129 // Notice that we call 'begin' etc with qualification.
131 iter i
= boost::begin( pair
);
132 iter e
= boost::end( pair
);
133 i
= boost::begin( cpair
);
134 e
= boost::end( cpair
);
135 boost::range_size
< Foo::Pair
<iter
> >::type s
= boost::size( pair
);
136 s
= boost::size( cpair
);
137 boost::range_const_reverse_iterator
< Foo::Pair
<iter
> >::type
138 ri
= boost::rbegin( cpair
),
139 re
= boost::rend( cpair
);
142 // Test metafunctions
145 boost::range_value
< Foo::Pair
<iter
> >::type
146 v
= *boost::begin(pair
);
148 boost::range_difference
< Foo::Pair
<iter
> >::type
149 d
= boost::end(pair
) - boost::begin(pair
);