// Boost.Range library // // Copyright Thorsten Ottosen 2003-2008. Use, modification and // distribution is subject to the Boost Software License, Version // 1.0. (See accompanying file LICENSE_1_0.txt or copy at // http://www.boost.org/LICENSE_1_0.txt) // // For more information, see http://www.boost.org/libs/range/ // #include #include // for std::iterator_traits, std::distance() namespace Foo { // // Our sample UDT. A 'Pair' // will work as a range when the stored // elements are iterators. // template< class T > struct Pair { T first, last; }; } // namespace 'Foo' namespace boost { // // Specialize metafunctions. We must include the range.hpp header. // We must open the 'boost' namespace. // /* template< class T > struct range_value< Foo::Pair > { typedef typename std::iterator_traits::value_type type; }; */ template< class T > struct range_iterator< Foo::Pair > { typedef T type; }; template< class T > struct range_const_iterator< Foo::Pair > { // // Remark: this is defined similar to 'range_iterator' // because the 'Pair' type does not distinguish // between an iterator and a const_iterator. // typedef T type; }; /* template< class T > struct range_difference< Foo::Pair > { typedef typename std::iterator_traits::difference_type type; }; */ template< class T > struct range_size< Foo::Pair > { int static_assertion[ sizeof( std::size_t ) >= sizeof( typename range_difference< Foo::Pair >::type ) ]; typedef std::size_t type; }; } // namespace 'boost' namespace Foo { // // The required functions. These should be defined in // the same namespace as 'Pair', in this case // in namespace 'Foo'. // template< class T > inline T boost_range_begin( Pair& x ) { return x.first; } template< class T > inline T boost_range_begin( const Pair& x ) { return x.first; } template< class T > inline T boost_range_end( Pair& x ) { return x.last; } template< class T > inline T boost_range_end( const Pair& x ) { return x.last; } template< class T > inline typename boost::range_size< Pair >::type boost_range_size( const Pair& x ) { return std::distance(x.first,x.last); } } // namespace 'Foo' #include int main() { typedef std::vector::iterator iter; std::vector vec; vec.push_back( 42 ); Foo::Pair pair = { vec.begin(), vec.end() }; const Foo::Pair& cpair = pair; // // Notice that we call 'begin' etc with qualification. // iter i = boost::begin( pair ); iter e = boost::end( pair ); i = boost::begin( cpair ); e = boost::end( cpair ); boost::range_size< Foo::Pair >::type s = boost::size( pair ); s = boost::size( cpair ); boost::range_const_reverse_iterator< Foo::Pair >::type ri = boost::rbegin( cpair ), re = boost::rend( cpair ); // // Test metafunctions // boost::range_value< Foo::Pair >::type v = *boost::begin(pair); boost::range_difference< Foo::Pair >::type d = boost::end(pair) - boost::begin(pair); }