iterator_archetypes.hpp, iterator_concepts.hpp -

incrementable_iterator_tag -> incrementable_traversal_tag
   single_pass_iterator_tag -> single_pass_traversal_tag

iterator_categories.hpp -

   added writability stripping to new_category_to_access for iterator

   adaptors based on iterators with new-style tags

   ReturnTag->AccessTag / returns->access

   Fixed a bug which would rule out user-defined access/traversal tags
   - we weren't accounting for tag convertibility without public
   inheritance.

iterator_facade.hpp -

   Workaround for a Borland const-dropping bug.

detail/categories.hpp -

   fixed is_tag so it doesn't rely on inheritance for detection

concept_tests.cpp -

   added new tests, use static_assert_same for better feedback on failure

iterator_adaptor_test.cpp

   workarounds for CWPro7, use static_assert_same for better feedback on failure
   wiped out #if 0 section

unit_tests.cpp -

   factored static_assert_same into a separate file


[SVN r1296]
This commit is contained in:
Dave Abrahams
2003-05-31 02:44:39 +00:00
parent e2b58d1eea
commit 65fe9a13e5
9 changed files with 306 additions and 157 deletions

View File

@@ -5,25 +5,24 @@
// to its suitability for any purpose. // to its suitability for any purpose.
#ifndef BOOST_ITERATOR_DETAIL_CATEGORIES_HPP #ifndef BOOST_ITERATOR_DETAIL_CATEGORIES_HPP
#define BOOST_ITERATOR_DETAIL_CATEGORIES_HPP # define BOOST_ITERATOR_DETAIL_CATEGORIES_HPP
#include <boost/config.hpp> # include <boost/config.hpp>
#include <boost/iterator/detail/config_def.hpp> # include <boost/iterator/detail/config_def.hpp>
#include <boost/detail/workaround.hpp> # include <boost/detail/workaround.hpp>
#include <boost/type_traits/is_base_and_derived.hpp> # include <boost/type_traits/is_convertible.hpp>
#include <boost/type_traits/is_convertible.hpp> # include <boost/type_traits/is_same.hpp>
#include <boost/type_traits/is_same.hpp>
#include <boost/mpl/if.hpp> # include <boost/mpl/if.hpp>
#include <boost/mpl/apply_if.hpp> # include <boost/mpl/apply_if.hpp>
#include <boost/mpl/identity.hpp> # include <boost/mpl/identity.hpp>
#include <boost/mpl/bool.hpp> # include <boost/mpl/bool.hpp>
#include <boost/mpl/or.hpp> # include <boost/mpl/or.hpp>
#include <boost/mpl/and.hpp> # include <boost/mpl/and.hpp>
#include <iterator> # include <iterator>
namespace boost namespace boost
{ {
@@ -78,19 +77,19 @@ namespace boost
// //
// Traversal Categories // Traversal Categories
// //
struct incrementable_iterator_tag struct incrementable_traversal_tag
{ {
typedef std::output_iterator_tag max_category; typedef std::output_iterator_tag max_category;
}; };
struct single_pass_iterator_tag struct single_pass_traversal_tag
: incrementable_iterator_tag : incrementable_traversal_tag
{ {
typedef detail::input_output_iterator_tag max_category; typedef detail::input_output_iterator_tag max_category;
}; };
struct forward_traversal_tag struct forward_traversal_tag
: single_pass_iterator_tag : single_pass_traversal_tag
{ {
typedef std::forward_iterator_tag max_category; typedef std::forward_iterator_tag max_category;
}; };
@@ -118,53 +117,36 @@ namespace boost
// I bet this is defined somewhere else. Let's wait and see. // I bet this is defined somewhere else. Let's wait and see.
struct error_type; struct error_type;
#ifndef BOOST_NO_IS_CONVERTIBLE # ifndef BOOST_NO_IS_CONVERTIBLE
template <class Base, class Derived>
struct is_same_or_derived
# ifdef BOOST_NO_TEMPLATE_PARTIAL_SPECIALIZATION
: mpl::or_<
is_same<Base, Derived>
, is_base_and_derived<Base, Derived>
>
{};
# else
: is_base_and_derived<Base, Derived>
{};
template <class T> struct is_same_or_derived<T,T> : mpl::true_ {};
# endif
// True iff T is a tag "derived" from Tag
template <class Tag, class T> template <class Tag, class T>
struct is_tag struct is_tag
: mpl::or_< : mpl::or_<
# ifdef BOOST_NO_TEMPLATE_PARTIAL_SPECIALIZATION is_convertible<T, Tag>
is_same_or_derived<Tag, T>
# else
is_base_and_derived<Tag, T>
# endif
// Because we can't actually get forward_iterator_tag to // Because we can't actually get forward_iterator_tag to
// derive from input_output_iterator_tag, we need this // derive from input_output_iterator_tag, we need this
// case. // case.
, mpl::and_< , mpl::and_<
is_same_or_derived<Tag,detail::input_output_iterator_tag> is_convertible<T,std::forward_iterator_tag>
, is_same_or_derived<std::forward_iterator_tag,T> , is_convertible<detail::input_output_iterator_tag,Tag>
> >
> >
{}; {};
#else # else
template <class Tag, class T> template <class Tag, class T>
struct is_tag; struct is_tag;
#endif # endif
// Generate specializations which will allow us to find // Generate specializations which will allow us to find
// null_category_tag as a minimum old-style category for new-style // null_category_tag as a minimum old-style category for new-style
// iterators which don't have an actual old-style category. We // iterators which don't have an actual old-style category. We
// need that so there is a valid base class for all new-style // need that so there is a valid base class for all new-style
// iterators. // iterators.
#define BOOST_OLD_ITERATOR_CATEGORY(category) \ # define BOOST_OLD_ITERATOR_CATEGORY(category) \
template <> \ template <> \
struct is_tag <detail::null_category_tag, std::category> \ struct is_tag <detail::null_category_tag, std::category> \
: mpl::true_ {}; : mpl::true_ {};
@@ -174,7 +156,7 @@ namespace boost
BOOST_OLD_ITERATOR_CATEGORY(forward_iterator_tag) BOOST_OLD_ITERATOR_CATEGORY(forward_iterator_tag)
BOOST_OLD_ITERATOR_CATEGORY(bidirectional_iterator_tag) BOOST_OLD_ITERATOR_CATEGORY(bidirectional_iterator_tag)
BOOST_OLD_ITERATOR_CATEGORY(random_access_iterator_tag) BOOST_OLD_ITERATOR_CATEGORY(random_access_iterator_tag)
#undef BOOST_OLD_ITERATOR_CATEGORY # undef BOOST_OLD_ITERATOR_CATEGORY
template <> template <>
struct is_tag<detail::input_output_iterator_tag,std::forward_iterator_tag> struct is_tag<detail::input_output_iterator_tag,std::forward_iterator_tag>
@@ -182,7 +164,7 @@ namespace boost
{ {
}; };
#ifndef BOOST_NO_TEMPLATE_PARTIAL_SPECIALIZATION # ifndef BOOST_NO_TEMPLATE_PARTIAL_SPECIALIZATION
template <class T> template <class T>
struct is_tag<T,T> : mpl::true_ struct is_tag<T,T> : mpl::true_
{}; {};
@@ -214,14 +196,14 @@ namespace boost
: is_tag_impl<Tag, T> : is_tag_impl<Tag, T>
{}; {};
# define BOOST_ITERATOR_DERIVED_TAG1(base, derived) \ # define BOOST_ITERATOR_DERIVED_TAG1(base, derived) \
BOOST_ITERATOR_DERIVED_TAG1_AUX(base, _, derived) BOOST_ITERATOR_DERIVED_TAG1_AUX(base, _, derived)
# define BOOST_ITERATOR_DERIVED_TAG1_AUX(base, underscore, derived) \ # define BOOST_ITERATOR_DERIVED_TAG1_AUX(base, underscore, derived) \
template<class T> \ template<class T> \
struct is_tag_impl<base##underscore##tag, T> \ struct is_tag_impl<base##underscore##tag, T> \
: is_tag<derived##underscore##tag, T> \ : is_tag<derived##underscore##tag, T> \
{ \ { \
}; };
// Old-style tag relations // Old-style tag relations
@@ -259,16 +241,65 @@ namespace boost
// Traversal tag relations // Traversal tag relations
BOOST_ITERATOR_DERIVED_TAG1(bidirectional_traversal, random_access_traversal) BOOST_ITERATOR_DERIVED_TAG1(bidirectional_traversal, random_access_traversal)
BOOST_ITERATOR_DERIVED_TAG1(forward_traversal, bidirectional_traversal) BOOST_ITERATOR_DERIVED_TAG1(forward_traversal, bidirectional_traversal)
BOOST_ITERATOR_DERIVED_TAG1(single_pass_iterator, forward_traversal) BOOST_ITERATOR_DERIVED_TAG1(single_pass_traversal, forward_traversal)
BOOST_ITERATOR_DERIVED_TAG1(incrementable_iterator, single_pass_iterator) BOOST_ITERATOR_DERIVED_TAG1(incrementable_traversal, single_pass_traversal)
# endif // BOOST_NO_IS_CONVERTIBLE workarounds # endif // BOOST_NO_IS_CONVERTIBLE workarounds
#else // BOOST_NO_TEMPLATE_PARTIAL_SPECIALIZATION case # endif // ndef BOOST_NO_TEMPLATE_PARTIAL_SPECIALIZATION
template <class Tag, class Known, class Else>
struct known_tag
: mpl::apply_if<is_tag<Known,Tag>, mpl::identity<Known>, Else>
{};
#endif template <class Tag>
struct max_known_traversal_tag
: known_tag<
Tag, random_access_traversal_tag
, known_tag<
Tag, bidirectional_traversal_tag
, known_tag<
Tag, forward_traversal_tag
, known_tag<
Tag, single_pass_traversal_tag
, known_tag<
Tag, incrementable_traversal_tag
, error_iterator_tag
>
>
>
>
>
{};
// Doesn't cope with these odd combinations: readable+swappable,
// writable+swappable. That doesn't matter for the sake of
// new-style tag base computation, which is all it's used for
// anyway.
template <class Tag>
struct max_known_access_tag
: known_tag<
Tag, writable_lvalue_iterator_tag
, known_tag<
Tag, readable_lvalue_iterator_tag
, known_tag<
Tag, readable_writable_iterator_tag
, known_tag<
Tag, writable_iterator_tag
, known_tag<
Tag, readable_iterator_tag
, mpl::apply_if<
is_tag<Tag, swappable_iterator_tag>
, mpl::identity<null_category_tag>
, error_iterator_tag
>
>
>
>
>
>
{};
// //
// Returns the minimum category type or error_type // Returns the minimum category type or error_type

View File

@@ -90,7 +90,7 @@ namespace boost
{}; {};
template <> template <>
struct traversal_archetype_impl<incrementable_iterator_tag> struct traversal_archetype_impl<incrementable_traversal_tag>
{ {
template<class Derived, class Value> template<class Derived, class Value>
struct archetype struct archetype
@@ -103,32 +103,32 @@ namespace boost
}; };
template <> template <>
struct traversal_archetype_impl<single_pass_iterator_tag> struct traversal_archetype_impl<single_pass_traversal_tag>
{ {
template<class Derived, class Value> template<class Derived, class Value>
struct archetype struct archetype
: public equality_comparable< traversal_archetype_<Derived, Value, single_pass_iterator_tag> >, : public equality_comparable< traversal_archetype_<Derived, Value, single_pass_traversal_tag> >,
public traversal_archetype_<Derived, Value, incrementable_iterator_tag> public traversal_archetype_<Derived, Value, incrementable_traversal_tag>
{ {
}; };
}; };
template <class Derived, class Value> template <class Derived, class Value>
bool operator==(traversal_archetype_<Derived, Value, single_pass_iterator_tag> const&, bool operator==(traversal_archetype_<Derived, Value, single_pass_traversal_tag> const&,
traversal_archetype_<Derived, Value, single_pass_iterator_tag> const&); traversal_archetype_<Derived, Value, single_pass_traversal_tag> const&);
#if BOOST_WORKAROUND(BOOST_MSVC, <= 1300) #if BOOST_WORKAROUND(BOOST_MSVC, <= 1300)
// doesn't seem to pick up != from equality_comparable // doesn't seem to pick up != from equality_comparable
template <class Derived, class Value> template <class Derived, class Value>
bool operator!=(traversal_archetype_<Derived, Value, single_pass_iterator_tag> const&, bool operator!=(traversal_archetype_<Derived, Value, single_pass_traversal_tag> const&,
traversal_archetype_<Derived, Value, single_pass_iterator_tag> const&); traversal_archetype_<Derived, Value, single_pass_traversal_tag> const&);
#endif #endif
template <> template <>
struct traversal_archetype_impl<forward_traversal_tag> struct traversal_archetype_impl<forward_traversal_tag>
{ {
template<class Derived, class Value> template<class Derived, class Value>
struct archetype struct archetype
: public traversal_archetype_<Derived, Value, single_pass_iterator_tag> : public traversal_archetype_<Derived, Value, single_pass_traversal_tag>
{ {
typedef std::ptrdiff_t difference_type; typedef std::ptrdiff_t difference_type;
}; };

View File

@@ -98,7 +98,7 @@ namespace boost {
template <> template <>
struct std_to_new_tags<std::input_iterator_tag> struct std_to_new_tags<std::input_iterator_tag>
{ {
typedef single_pass_iterator_tag type; typedef single_pass_traversal_tag type;
template <class Reference> template <class Reference>
struct apply struct apply
@@ -109,7 +109,7 @@ namespace boost {
template <> template <>
struct std_to_new_tags<std::output_iterator_tag> struct std_to_new_tags<std::output_iterator_tag>
{ {
typedef incrementable_iterator_tag type; typedef incrementable_traversal_tag type;
template <class Reference> template <class Reference>
struct apply struct apply
@@ -227,22 +227,55 @@ namespace boost {
namespace detail { namespace detail {
template <class NewCategoryTag>
struct get_access_category {
typedef typename NewCategoryTag::returns type;
};
template <class NewCategoryTag> template <class NewCategoryTag>
struct get_traversal_category { struct get_traversal_category {
typedef typename NewCategoryTag::traversal type; typedef typename NewCategoryTag::traversal type;
}; };
// Remove all writability from the given access tag. This
// functionality is part of new_category_to_access in order to
// support deduction of the proper default access category for
// iterator_adaptor; when the reference type is a reference to
// constant we must strip writability.
template <class AccessTag>
struct remove_access_writability
: mpl::apply_if<
is_tag<writable_lvalue_iterator_tag, AccessTag>
, mpl::identity<readable_lvalue_iterator_tag>
, mpl::apply_if<
is_tag<readable_writable_iterator_tag, AccessTag>
, mpl::identity<readable_iterator_tag>
, mpl::if_<
is_tag<writable_iterator_tag, AccessTag>
// Is this OK? I think it may correct be for all
// legitimate cases, because at this point the
// iterator is not readable, so it could not have
// been any more than writable + swappable.
, swappable_iterator_tag
, AccessTag
>
>
>
{};
template <class NewCategoryTag, class Reference>
struct new_category_to_access
: mpl::apply_if<
python::detail::is_reference_to_const<Reference>
, remove_access_writability<typename NewCategoryTag::access>
, mpl::identity<typename NewCategoryTag::access>
>
{};
template <class CategoryTag, class Reference> template <class CategoryTag, class Reference>
struct access_category_tag struct access_category_tag
: mpl::apply_if< : mpl::apply_if<
is_new_iterator_tag<CategoryTag> is_new_iterator_tag<CategoryTag>
, get_access_category<CategoryTag> , new_category_to_access<CategoryTag, Reference>
, iter_category_to_access<CategoryTag, Reference> , iter_category_to_access<CategoryTag, Reference>
> >
{ {
}; };
@@ -262,6 +295,34 @@ namespace boost {
template <> struct traversal_category_tag<int> { typedef void type; }; template <> struct traversal_category_tag<int> { typedef void type; };
# endif # endif
// iterator_tag_base - a metafunction to compute the appropriate
// old-style tag (if any) to use as a base for a new-style tag.
template <class KnownAccessTag, class KnownTraversalTag>
struct iterator_tag_base
: minimum_category<
typename KnownAccessTag::max_category
, typename KnownTraversalTag::max_category
>
{};
# if BOOST_WORKAROUND(BOOST_MSVC,<=1200)
template <>
struct iterator_tag_base<int,int>
: mpl::false_ {}; // just using false_ so that the result will be
// a legal base class
# endif
// specialization for this special case. Otherwise we get
// input_output_iterator_tag, because the standard hierarchy has a
// sudden anomalous distinction between readability and
// writability at the level of input iterator/output iterator.
template <>
struct iterator_tag_base<
readable_lvalue_iterator_tag,single_pass_traversal_tag>
{
typedef std::input_iterator_tag type;
};
} // namespace detail } // namespace detail
template <class Iterator> template <class Iterator>
@@ -279,7 +340,7 @@ namespace boost {
{ {
}; };
#if !defined(BOOST_NO_TEMPLATE_PARTIAL_SPECIALIZATION) # if !defined(BOOST_NO_TEMPLATE_PARTIAL_SPECIALIZATION)
template <typename T> template <typename T>
struct access_category<T*> struct access_category<T*>
@@ -296,18 +357,16 @@ namespace boost {
typedef random_access_traversal_tag type; typedef random_access_traversal_tag type;
}; };
#endif # endif
template <class ReturnTag, class TraversalTag> template <class AccessTag, class TraversalTag>
struct iterator_tag struct iterator_tag
: mpl::aux::msvc_eti_base< : detail::iterator_tag_base<
typename detail::minimum_category< typename detail::max_known_access_tag<AccessTag>::type
typename ReturnTag::max_category , typename detail::max_known_traversal_tag<TraversalTag>::type
, typename TraversalTag::max_category >::type
>::type
>::type
{ {
typedef ReturnTag returns; typedef AccessTag access;
typedef TraversalTag traversal; typedef TraversalTag traversal;
}; };

View File

@@ -164,7 +164,7 @@ namespace boost_concepts {
boost::function_requires< boost::function_requires<
boost::DefaultConstructibleConcept<Iterator> >(); boost::DefaultConstructibleConcept<Iterator> >();
BOOST_STATIC_ASSERT((boost::detail::is_tag<boost::incrementable_iterator_tag, traversal_category>::value)); BOOST_STATIC_ASSERT((boost::detail::is_tag<boost::incrementable_traversal_tag, traversal_category>::value));
++i; ++i;
(void)i++; (void)i++;
@@ -182,7 +182,7 @@ namespace boost_concepts {
boost::function_requires< IncrementableIteratorConcept<Iterator> >(); boost::function_requires< IncrementableIteratorConcept<Iterator> >();
boost::function_requires< boost::EqualityComparableConcept<Iterator> >(); boost::function_requires< boost::EqualityComparableConcept<Iterator> >();
BOOST_STATIC_ASSERT((boost::detail::is_tag<boost::single_pass_iterator_tag, traversal_category>::value)); BOOST_STATIC_ASSERT((boost::detail::is_tag<boost::single_pass_traversal_tag, traversal_category>::value));
} }
}; };
@@ -254,7 +254,7 @@ namespace detail
struct Operations; struct Operations;
template <> template <>
struct Operations<boost::incrementable_iterator_tag> struct Operations<boost::incrementable_traversal_tag>
{ {
template <typename Iterator1, typename Iterator2> template <typename Iterator1, typename Iterator2>
static void constraints(Iterator1 const& i1, Iterator2 const& i2) static void constraints(Iterator1 const& i1, Iterator2 const& i2)
@@ -264,12 +264,12 @@ namespace detail
}; };
template <> template <>
struct Operations<boost::single_pass_iterator_tag> struct Operations<boost::single_pass_traversal_tag>
{ {
template <typename Iterator1, typename Iterator2> template <typename Iterator1, typename Iterator2>
static void constraints(Iterator1 const& i1, Iterator2 const& i2) static void constraints(Iterator1 const& i1, Iterator2 const& i2)
{ {
Operations<boost::incrementable_iterator_tag>(i1, i2); Operations<boost::incrementable_traversal_tag>(i1, i2);
i1 == i2; i1 == i2;
i1 != i2; i1 != i2;
@@ -284,7 +284,7 @@ namespace detail
template <typename Iterator1, typename Iterator2> template <typename Iterator1, typename Iterator2>
static void constraints(Iterator1 const& i1, Iterator2 const& i2) static void constraints(Iterator1 const& i1, Iterator2 const& i2)
{ {
Operations<boost::single_pass_iterator_tag>::constraints(i1, i2); Operations<boost::single_pass_traversal_tag>::constraints(i1, i2);
} }
}; };

View File

@@ -62,11 +62,22 @@ namespace boost
}; };
//
// Add const qualification for iterators which are not writable
//
template<class Value, class AccessCategory> template<class Value, class AccessCategory>
struct const_qualified : struct const_qualified_ref :
mpl::if_< is_tag< writable_iterator_tag, AccessCategory >, mpl::if_< is_tag< writable_iterator_tag, AccessCategory >,
Value, Value&,
Value const > Value const& >
{};
// The apparent duplication here works around a Borland problem
template<class Value, class AccessCategory>
struct const_qualified_ptr :
mpl::if_< is_tag< writable_iterator_tag, AccessCategory >,
Value*,
Value const* >
{}; {};
// //
@@ -84,7 +95,7 @@ namespace boost
, Difference , Difference
, typename const_qualified<Value, AccessCategory>::type* , typename const_qualified_ptr<Value, AccessCategory>::type
// The use_default support is needed for iterator_adaptor. // The use_default support is needed for iterator_adaptor.
// For practical reasons iterator_adaptor needs to specify // For practical reasons iterator_adaptor needs to specify
@@ -93,7 +104,7 @@ namespace boost
// is your default parameter". // is your default parameter".
, typename mpl::if_< , typename mpl::if_<
is_same<Reference, use_default> is_same<Reference, use_default>
, typename const_qualified<Value, AccessCategory>::type& , typename const_qualified_ref<Value, AccessCategory>::type
, Reference , Reference
>::type >::type
> >
@@ -333,7 +344,7 @@ namespace boost
, class Value , class Value
, class AccessCategory , class AccessCategory
, class TraversalCategory , class TraversalCategory
, class Reference = typename detail::const_qualified<Value, AccessCategory>::type& , class Reference = typename detail::const_qualified_ref<Value, AccessCategory>::type
, class Difference = std::ptrdiff_t , class Difference = std::ptrdiff_t
> >
class iterator_facade class iterator_facade

View File

@@ -7,6 +7,8 @@
#include <boost/iterator/iterator_concepts.hpp> #include <boost/iterator/iterator_concepts.hpp>
#include <boost/operators.hpp> #include <boost/operators.hpp>
#include <boost/static_assert.hpp> // remove #include <boost/static_assert.hpp> // remove
#include <boost/detail/workaround.hpp>
#include "static_assert_same.hpp" // remove
struct new_iterator struct new_iterator
: public boost::iterator< boost::iterator_tag< : public boost::iterator< boost::iterator_tag<
@@ -50,13 +52,26 @@ struct old_iterator
}; };
old_iterator operator+(std::ptrdiff_t, old_iterator x) { return x; } old_iterator operator+(std::ptrdiff_t, old_iterator x) { return x; }
struct my_writable_lvalue_iterator_tag
{
operator boost::writable_lvalue_iterator_tag() const;
};
struct my_single_pass_traversal_tag
{
operator boost::single_pass_traversal_tag() const;
};
void test_tag_convertibility() void test_tag_convertibility()
{ {
#ifndef BOOST_NO_IS_CONVERTIBLE // This set of tests is by no means complete.
// Test that this is an input/output iterator
#if !BOOST_WORKAROUND(__MWERKS__, <= 0x2407)
{ {
typedef boost::iterator_tag< typedef boost::iterator_tag<
boost::writable_lvalue_iterator_tag boost::writable_lvalue_iterator_tag
, boost::single_pass_iterator_tag , boost::single_pass_traversal_tag
> tag; > tag;
BOOST_STATIC_ASSERT(( BOOST_STATIC_ASSERT((
@@ -69,10 +84,33 @@ void test_tag_convertibility()
!boost::is_convertible<tag, std::forward_iterator_tag>::value !boost::is_convertible<tag, std::forward_iterator_tag>::value
)); ));
} }
// Test that it's possible to build new sub-tags without
// derivation. Convertibility should be enough
{
typedef boost::iterator_tag<
my_writable_lvalue_iterator_tag
, my_single_pass_traversal_tag
> tag;
BOOST_STATIC_ASSERT((
boost::is_convertible<tag, std::output_iterator_tag>::value
));
BOOST_STATIC_ASSERT((
boost::is_convertible<tag, std::input_iterator_tag>::value
));
BOOST_STATIC_ASSERT((
!boost::is_convertible<tag, std::forward_iterator_tag>::value
));
}
// Test that a single-pass readable lvalue iterator is only an
// input iterator. Requires special case handling in
// categories.hpp
{ {
typedef boost::iterator_tag< typedef boost::iterator_tag<
boost::readable_lvalue_iterator_tag boost::readable_lvalue_iterator_tag
, boost::single_pass_iterator_tag , boost::single_pass_traversal_tag
> tag; > tag;
BOOST_STATIC_ASSERT(( BOOST_STATIC_ASSERT((
boost::is_convertible<tag, std::input_iterator_tag>::value boost::is_convertible<tag, std::input_iterator_tag>::value
@@ -95,12 +133,12 @@ main()
typedef boost::iterator_tag< boost::writable_lvalue_iterator_tag, boost::random_access_traversal_tag > tag; typedef boost::iterator_tag< boost::writable_lvalue_iterator_tag, boost::random_access_traversal_tag > tag;
// BOOST_STATIC_ASSERT((boost::detail::is_random_access_iterator<tag>::value)); // BOOST_STATIC_ASSERT((boost::detail::is_random_access_iterator<tag>::value));
BOOST_STATIC_ASSERT((boost::is_same<tag::returns, boost::writable_lvalue_iterator_tag>::value)); int test = static_assert_same<tag::access, boost::writable_lvalue_iterator_tag>::value;
BOOST_STATIC_ASSERT((boost::is_same<tag::traversal, boost::random_access_traversal_tag>::value)); test = static_assert_same<tag::traversal, boost::random_access_traversal_tag>::value;
// BOOST_STATIC_ASSERT((boost::detail::is_random_access_iterator<new_iterator::iterator_category>::value)); // BOOST_STATIC_ASSERT((boost::detail::is_random_access_iterator<new_iterator::iterator_category>::value));
BOOST_STATIC_ASSERT((boost::is_same<new_iterator::iterator_category::returns, boost::writable_lvalue_iterator_tag>::value)); test = static_assert_same<new_iterator::iterator_category::access, boost::writable_lvalue_iterator_tag>::value;
BOOST_STATIC_ASSERT((boost::is_same<new_iterator::iterator_category::traversal, boost::random_access_traversal_tag>::value)); test = static_assert_same<new_iterator::iterator_category::traversal, boost::random_access_traversal_tag>::value;
typedef boost::traversal_category<new_iterator>::type traversal_category; typedef boost::traversal_category<new_iterator>::type traversal_category;
@@ -108,8 +146,8 @@ main()
BOOST_STATIC_ASSERT(boost::detail::is_new_iterator_tag<new_iterator::iterator_category>::value); BOOST_STATIC_ASSERT(boost::detail::is_new_iterator_tag<new_iterator::iterator_category>::value);
BOOST_STATIC_ASSERT((boost::is_same<traversal_category, boost::random_access_traversal_tag>::value)); test = static_assert_same<traversal_category, boost::random_access_traversal_tag>::value;
(void)test;
#if !defined(BOOST_NO_TEMPLATE_PARTIAL_SPECIALIZATION) #if !defined(BOOST_NO_TEMPLATE_PARTIAL_SPECIALIZATION)
boost::function_requires< boost::function_requires<

View File

@@ -13,15 +13,6 @@
#include <functional> #include <functional>
#include <numeric> #include <numeric>
#if 0
#include <boost/iterator_adaptors.hpp>
#include <boost/generator_iterator.hpp>
#include <boost/pending/integer_range.hpp>
#include <boost/concept_archetype.hpp>
#include <boost/type_traits/same_traits.hpp>
#include <boost/permutation_iterator.hpp>
#endif
#include <boost/iterator/iterator_adaptor.hpp> #include <boost/iterator/iterator_adaptor.hpp>
#include <boost/pending/iterator_tests.hpp> #include <boost/pending/iterator_tests.hpp>
@@ -31,6 +22,8 @@
#include <set> #include <set>
#include <list> #include <list>
#include "static_assert_same.hpp"
struct my_iterator_tag : public std::random_access_iterator_tag { }; struct my_iterator_tag : public std::random_access_iterator_tag { };
using boost::dummyT; using boost::dummyT;
@@ -163,9 +156,15 @@ struct constant_iterator
, typename std::iterator_traits<Iter>::value_type const , typename std::iterator_traits<Iter>::value_type const
> >
{ {
typedef boost::iterator_adaptor<
constant_iterator<Iter>
, Iter
, typename std::iterator_traits<Iter>::value_type const
> base_t;
constant_iterator() {} constant_iterator() {}
constant_iterator(Iter it) constant_iterator(Iter it)
: constant_iterator::iterator_adaptor(it) {} : base_t(it) {}
}; };
int int
@@ -188,15 +187,16 @@ main()
boost::const_nonconst_iterator_test(i, ++j); boost::const_nonconst_iterator_test(i, ++j);
} }
int test;
// Test the iterator_traits // Test the iterator_traits
{ {
// Test computation of defaults // Test computation of defaults
typedef ptr_iterator<int> Iter1; typedef ptr_iterator<int> Iter1;
// don't use std::iterator_traits here to avoid VC++ problems // don't use std::iterator_traits here to avoid VC++ problems
BOOST_STATIC_ASSERT((boost::is_same<Iter1::value_type, int>::value)); test = static_assert_same<Iter1::value_type, int>::value;
BOOST_STATIC_ASSERT((boost::is_same<Iter1::reference, int&>::value)); test = static_assert_same<Iter1::reference, int&>::value;
BOOST_STATIC_ASSERT((boost::is_same<Iter1::pointer, int*>::value)); test = static_assert_same<Iter1::pointer, int*>::value;
BOOST_STATIC_ASSERT((boost::is_same<Iter1::difference_type, std::ptrdiff_t>::value)); test = static_assert_same<Iter1::difference_type, std::ptrdiff_t>::value;
#if !BOOST_WORKAROUND(__MWERKS__, <= 0x2407) #if !BOOST_WORKAROUND(__MWERKS__, <= 0x2407)
BOOST_STATIC_ASSERT((boost::is_convertible<Iter1::iterator_category, std::random_access_iterator_tag>::value)); BOOST_STATIC_ASSERT((boost::is_convertible<Iter1::iterator_category, std::random_access_iterator_tag>::value));
#endif #endif
@@ -205,14 +205,9 @@ main()
{ {
// Test computation of default when the Value is const // Test computation of default when the Value is const
typedef ptr_iterator<int const> Iter1; typedef ptr_iterator<int const> Iter1;
BOOST_STATIC_ASSERT((boost::is_same<Iter1::value_type, int>::value)); test = static_assert_same<Iter1::value_type, int>::value;
BOOST_STATIC_ASSERT((boost::is_same<Iter1::reference, const int&>::value)); test = static_assert_same<Iter1::reference, const int&>::value;
BOOST_STATIC_ASSERT((boost::is_same<Iter1::iterator_category::returns, boost::readable_lvalue_iterator_tag>::value)); test = static_assert_same<Iter1::iterator_category::access, boost::readable_lvalue_iterator_tag>::value; test = static_assert_same<Iter1::pointer, const int*>::value;
#if !BOOST_WORKAROUND(__BORLANDC__, BOOST_TESTED_AT(0x551))
// Borland has known problems with const
BOOST_STATIC_ASSERT((boost::is_same<Iter1::pointer, const int*>::value));
#endif
} }
{ {
@@ -220,12 +215,12 @@ main()
typedef ptr_iterator<int> BaseIter; typedef ptr_iterator<int> BaseIter;
typedef constant_iterator<BaseIter> Iter; typedef constant_iterator<BaseIter> Iter;
BOOST_STATIC_ASSERT((boost::is_same<Iter::value_type, int>::value)); test = static_assert_same<Iter::value_type, int>::value;
BOOST_STATIC_ASSERT((boost::is_same<Iter::reference, int const&>::value)); test = static_assert_same<Iter::reference, int const&>::value;
BOOST_STATIC_ASSERT((boost::is_same<Iter::pointer, int const*>::value)); test = static_assert_same<Iter::pointer, int const*>::value;
BOOST_STATIC_ASSERT((boost::is_same<BaseIter::iterator_category::returns, boost::writable_lvalue_iterator_tag>::value)); test = static_assert_same<BaseIter::iterator_category::access, boost::writable_lvalue_iterator_tag>::value;
BOOST_STATIC_ASSERT((boost::is_same<Iter::iterator_category::returns, boost::readable_lvalue_iterator_tag>::value)); test = static_assert_same<Iter::iterator_category::access, boost::readable_lvalue_iterator_tag>::value;
} }
// Test the iterator_adaptor // Test the iterator_adaptor
@@ -261,5 +256,6 @@ main()
} }
std::cout << "test successful " << std::endl; std::cout << "test successful " << std::endl;
(void)test;
return 0; return 0;
} }

31
test/static_assert_same.hpp Executable file
View File

@@ -0,0 +1,31 @@
// Copyright David Abrahams 2003. Permission to copy, use,
// modify, sell and distribute this software is granted provided this
// copyright notice appears in all copies. This software is provided
// "as is" without express or implied warranty, and with no claim as
// to its suitability for any purpose.
#ifndef STATIC_ASSERT_SAME_DWA2003530_HPP
# define STATIC_ASSERT_SAME_DWA2003530_HPP
# include <boost/type.hpp>
#ifndef BOOST_NO_TEMPLATE_PARTIAL_SPECIALIZATION
template <class T, class U>
struct static_assert_same;
template <class T>
struct static_assert_same<T,T>
{
enum { value = 1 };
};
#else
# include <boost/mpl/if.hpp>
# include <boost/mpl/bool.hpp>
# include <boost/type_traits/is_same.hpp>
template <class T, class U>
struct static_assert_same
: boost::mpl::if_<boost::is_same<T,U>,boost::mpl::true_,void>::type
{};
#endif
#endif // STATIC_ASSERT_SAME_DWA2003530_HPP

View File

@@ -5,7 +5,7 @@
// to its suitability for any purpose. // to its suitability for any purpose.
#include <boost/iterator/iterator_adaptor.hpp> #include <boost/iterator/iterator_adaptor.hpp>
#include <boost/static_assert.hpp> #include <boost/static_assert.hpp>
#include <boost/type.hpp> #include "static_assert_same.hpp"
struct X { int a; }; struct X { int a; };
@@ -30,25 +30,6 @@ void operator_arrow_test()
take_xptr(Xiter(&x).operator->()); take_xptr(Xiter(&x).operator->());
} }
template <class T, class U>
struct static_assert_same
#ifdef BOOST_NO_TEMPLATE_PARTIAL_SPECIALIZATION
{
template <class V>
static int check(V, V);
enum { value = sizeof(check(boost::type<T>(), boost::type<U>())) };
}
#endif
;
#ifndef BOOST_NO_TEMPLATE_PARTIAL_SPECIALIZATION
template <class T>
struct static_assert_same<T,T>
{
enum { value };
};
#endif
template <class T, class U, class Min> template <class T, class U, class Min>
struct static_assert_min_cat struct static_assert_min_cat
: static_assert_same< : static_assert_same<
@@ -109,6 +90,8 @@ void category_test()
test = static_assert_min_cat< test = static_assert_min_cat<
std::output_iterator_tag,std::random_access_iterator_tag, std::output_iterator_tag std::output_iterator_tag,std::random_access_iterator_tag, std::output_iterator_tag
>::value; >::value;
(void)test;
} }
int main() int main()