mirror of
https://github.com/boostorg/utility.git
synced 2025-05-09 15:04:00 +00:00
Cleanups on boost::iterator_traits<>
Broke MSVC though :( [SVN r16157]
This commit is contained in:
parent
f0ea53e77e
commit
9655beb7ba
@ -1,4 +1,4 @@
|
|||||||
// (C) Copyright David Abrahams 2001. Permission to copy, use, modify,
|
// (C) Copyright David Abrahams 2002. Permission to copy, use, modify,
|
||||||
// sell and distribute this software is granted provided this
|
// sell and distribute this software is granted provided this
|
||||||
// copyright notice appears in all copies. This software is provided
|
// copyright notice appears in all copies. This software is provided
|
||||||
// "as is" without express or implied warranty, and with no claim as
|
// "as is" without express or implied warranty, and with no claim as
|
||||||
@ -22,7 +22,7 @@
|
|||||||
// 19 Jan 2001 Initial version with iterator operators (David Abrahams)
|
// 19 Jan 2001 Initial version with iterator operators (David Abrahams)
|
||||||
|
|
||||||
#include <boost/detail/iterator.hpp>
|
#include <boost/detail/iterator.hpp>
|
||||||
#include <boost/type_traits.hpp>
|
#include <boost/type_traits/is_same.hpp>
|
||||||
#include <boost/operators.hpp>
|
#include <boost/operators.hpp>
|
||||||
#include <boost/static_assert.hpp>
|
#include <boost/static_assert.hpp>
|
||||||
#include <iterator>
|
#include <iterator>
|
||||||
@ -31,6 +31,11 @@
|
|||||||
#include <cassert>
|
#include <cassert>
|
||||||
#include <iostream>
|
#include <iostream>
|
||||||
|
|
||||||
|
// A UDT for which we can specialize std::iterator_traits<element*> on
|
||||||
|
// compilers which don't support partial specialization. There's no
|
||||||
|
// other reasonable way to test pointers on those compilers.
|
||||||
|
struct element {};
|
||||||
|
|
||||||
// An iterator for which we can get traits.
|
// An iterator for which we can get traits.
|
||||||
struct my_iterator1
|
struct my_iterator1
|
||||||
: boost::forward_iterator_helper<my_iterator1, char, long, const char*, const char&>
|
: boost::forward_iterator_helper<my_iterator1, char, long, const char*, const char&>
|
||||||
@ -77,46 +82,38 @@ struct my_iterator2
|
|||||||
struct my_iterator3 : my_iterator1
|
struct my_iterator3 : my_iterator1
|
||||||
{
|
{
|
||||||
typedef int difference_type;
|
typedef int difference_type;
|
||||||
my_iterator3(const char* p) : my_iterator1(p) {}
|
my_iterator3(const char* p)
|
||||||
|
: my_iterator1(p) {}
|
||||||
};
|
};
|
||||||
|
|
||||||
|
template <bool> struct assertion;
|
||||||
|
template <> struct assertion<true> { typedef char type; };
|
||||||
|
|
||||||
template <class Iterator,
|
template <class Iterator,
|
||||||
class value_type, class difference_type, class pointer, class reference, class category>
|
class value_type, class difference_type, class pointer, class reference, class category>
|
||||||
struct non_portable_tests
|
struct non_portable_tests
|
||||||
{
|
{
|
||||||
// Unfortunately, the VC6 standard library doesn't supply these :(
|
|
||||||
typedef typename boost::detail::iterator_traits<Iterator>::pointer test_pt;
|
typedef typename boost::detail::iterator_traits<Iterator>::pointer test_pt;
|
||||||
typedef typename boost::detail::iterator_traits<Iterator>::reference test_rt;
|
typedef typename boost::detail::iterator_traits<Iterator>::reference test_rt;
|
||||||
BOOST_STATIC_ASSERT((
|
|
||||||
::boost::is_same<
|
non_portable_tests()
|
||||||
test_pt,
|
{
|
||||||
pointer
|
BOOST_STATIC_ASSERT((::boost::is_same<test_pt, pointer>::value));
|
||||||
>::value));
|
BOOST_STATIC_ASSERT((::boost::is_same<test_rt, reference>::value));
|
||||||
|
}
|
||||||
BOOST_STATIC_ASSERT((
|
|
||||||
::boost::is_same<
|
|
||||||
test_rt,
|
|
||||||
reference
|
|
||||||
>::value));
|
|
||||||
};
|
};
|
||||||
|
|
||||||
template <class Iterator,
|
template <class Iterator,
|
||||||
class value_type, class difference_type, class pointer, class reference, class category>
|
class value_type, class difference_type, class pointer, class reference, class category>
|
||||||
struct portable_tests
|
struct portable_tests
|
||||||
{
|
{
|
||||||
typedef typename boost::detail::iterator_traits<Iterator>::difference_type test_dt;
|
portable_tests()
|
||||||
typedef typename boost::detail::iterator_traits<Iterator>::iterator_category test_cat;
|
{
|
||||||
BOOST_STATIC_ASSERT((
|
typedef typename boost::detail::iterator_traits<Iterator>::difference_type test_dt;
|
||||||
::boost::is_same<
|
typedef typename boost::detail::iterator_traits<Iterator>::iterator_category test_cat;
|
||||||
test_dt,
|
BOOST_STATIC_ASSERT((::boost::is_same<test_dt, difference_type>::value));
|
||||||
difference_type
|
BOOST_STATIC_ASSERT((::boost::is_same<test_cat, category>::value));
|
||||||
>::value));
|
}
|
||||||
|
|
||||||
BOOST_STATIC_ASSERT((
|
|
||||||
::boost::is_same<
|
|
||||||
test_cat,
|
|
||||||
category
|
|
||||||
>::value));
|
|
||||||
};
|
};
|
||||||
|
|
||||||
// Test iterator_traits
|
// Test iterator_traits
|
||||||
@ -125,12 +122,15 @@ template <class Iterator,
|
|||||||
struct input_iterator_test
|
struct input_iterator_test
|
||||||
: portable_tests<Iterator,value_type,difference_type,pointer,reference,category>
|
: portable_tests<Iterator,value_type,difference_type,pointer,reference,category>
|
||||||
{
|
{
|
||||||
typedef typename boost::detail::iterator_traits<Iterator>::value_type test_vt;
|
input_iterator_test()
|
||||||
BOOST_STATIC_ASSERT((
|
{
|
||||||
::boost::is_same<
|
typedef typename boost::detail::iterator_traits<Iterator>::value_type test_vt;
|
||||||
test_vt,
|
BOOST_STATIC_ASSERT((
|
||||||
value_type
|
::boost::is_same<
|
||||||
>::value));
|
test_vt,
|
||||||
|
value_type
|
||||||
|
>::value));
|
||||||
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
template <class Iterator,
|
template <class Iterator,
|
||||||
@ -154,14 +154,13 @@ struct maybe_pointer_test
|
|||||||
input_iterator_test<std::istream_iterator<int>, int, std::ptrdiff_t, int*, int&, std::input_iterator_tag>
|
input_iterator_test<std::istream_iterator<int>, int, std::ptrdiff_t, int*, int&, std::input_iterator_tag>
|
||||||
istream_iterator_test;
|
istream_iterator_test;
|
||||||
|
|
||||||
//
|
|
||||||
#if defined(__BORLANDC__) && !defined(__SGI_STL_PORT)
|
#if defined(__BORLANDC__) && !defined(__SGI_STL_PORT)
|
||||||
typedef ::std::char_traits<char>::off_type distance;
|
typedef ::std::char_traits<char>::off_type distance;
|
||||||
non_pointer_test<std::ostream_iterator<int>,int,
|
non_pointer_test<std::ostream_iterator<int>,int,
|
||||||
distance,int*,int&,std::output_iterator_tag> ostream_iterator_test;
|
distance,int*,int&,std::output_iterator_tag> ostream_iterator_test;
|
||||||
#elif defined(BOOST_MSVC_STD_ITERATOR)
|
#elif defined(BOOST_MSVC_STD_ITERATOR)
|
||||||
non_pointer_test<std::ostream_iterator<int>,
|
non_pointer_test<std::ostream_iterator<int>,
|
||||||
int, void, void, void, std::output_iterator_tag>
|
int, void, int*, int&, std::output_iterator_tag>
|
||||||
ostream_iterator_test;
|
ostream_iterator_test;
|
||||||
#else
|
#else
|
||||||
non_pointer_test<std::ostream_iterator<int>,
|
non_pointer_test<std::ostream_iterator<int>,
|
||||||
@ -175,6 +174,7 @@ non_pointer_test<std::ostream_iterator<int>,
|
|||||||
#else
|
#else
|
||||||
typedef std::ptrdiff_t std_list_diff_type;
|
typedef std::ptrdiff_t std_list_diff_type;
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
non_pointer_test<std::list<int>::iterator, int, std_list_diff_type, int*, int&, std::bidirectional_iterator_tag>
|
non_pointer_test<std::list<int>::iterator, int, std_list_diff_type, int*, int&, std::bidirectional_iterator_tag>
|
||||||
list_iterator_test;
|
list_iterator_test;
|
||||||
|
|
||||||
@ -192,13 +192,13 @@ non_pointer_test<my_iterator2, char, long, const char*, const char&, std::forwar
|
|||||||
|
|
||||||
non_pointer_test<my_iterator3, char, int, const char*, const char&, std::forward_iterator_tag>
|
non_pointer_test<my_iterator3, char, int, const char*, const char&, std::forward_iterator_tag>
|
||||||
my_iterator3_test;
|
my_iterator3_test;
|
||||||
|
|
||||||
int main()
|
int main()
|
||||||
{
|
{
|
||||||
char chars[100];
|
char chars[100];
|
||||||
int ints[100];
|
int ints[100];
|
||||||
|
|
||||||
for (std::ptrdiff_t length = 3; length < 100; length += length / 3)
|
for (int length = 3; length < 100; length += length / 3)
|
||||||
{
|
{
|
||||||
std::list<int> l(length);
|
std::list<int> l(length);
|
||||||
assert(boost::detail::distance(l.begin(), l.end()) == length);
|
assert(boost::detail::distance(l.begin(), l.end()) == length);
|
||||||
|
Loading…
x
Reference in New Issue
Block a user