Boost logo

Boost :

Subject: [boost] Any real-world need for static_cast_iterator, const_cast_iterator, dynamic_cast_iterator?
From: Nigel Stewart (nigels_at_[hidden])
Date: 2009-02-03 01:30:44


In addition to the specialised adaptors in the boost iterator
library, does it make any sense to add:

     static_cast_iterator
     const_cast_iterator
     dynamic_cast_iterator

...iterator adaptors providing embedded static_cast<>, const_cast<>
or dynamic_cast<> when dereferencing? (Rather than just throwing
them straight on the scrap heap...)

- Nigel

----------------------------------------------

   template<typename T, typename I>
   class static_cast_iterator :
     public boost::transform_iterator
     <
       T (*)(const typename I::reference),
       I
>
   {
   private:
     typedef typename I::reference F;
     typedef typename boost::transform_iterator< T (*)(const F), I> B;

   public:
     static_cast_iterator() {}
     static_cast_iterator(const I &i) : B(i,&doCast) {}

     operator const I &() const { return B::base(); }

     static_cast_iterator &operator++() { B::operator++(); return *this; }
     static_cast_iterator &operator--() { B::operator--(); return *this; }

   private:
     static T doCast(const F val) { return static_cast<T>(val); }
   };

   template<typename I>
   class const_cast_iterator :
     public boost::transform_iterator
     <
       typename boost::remove_const<typename I::reference>::type (*)(const typename I::reference),
       I
>
   {
   private:
     typedef typename I::reference F;
     typedef typename boost::remove_const<F>::type T;
     typedef typename boost::transform_iterator< T (*)(const F), I> B;

     BOOST_STATIC_ASSERT(boost::is_const<F>::value);
     BOOST_STATIC_ASSERT(!boost::is_const<T>::value);

   public:
     const_cast_iterator() {}
     const_cast_iterator(const I &i) : B(i,&doCast) {}

     operator const I &() const { return B::base(); }

     const_cast_iterator &operator++() { B::operator++(); return *this; }
     const_cast_iterator &operator--() { B::operator--(); return *this; }

   private:
     static T doCast(const F val) { return const_cast<T>(val); }
   };

   template<typename T, typename I>
   class dynamic_cast_iterator :
     public boost::transform_iterator
     <
       T (*)(const typename I::reference),
       I
>
   {
   private:
     typedef typename I::reference F;
     typedef typename boost::transform_iterator< T (*)(const F), I> B;

     BOOST_STATIC_ASSERT(boost::is_pointer<F>::value || boost::is_reference<F>::value);
     BOOST_STATIC_ASSERT(boost::is_pointer<T>::value || boost::is_reference<T>::value);

   public:
     dynamic_cast_iterator() {}
     dynamic_cast_iterator(const I &i) : B(i,&doCast) {}

     operator const I &() const { return B::base(); }

     dynamic_cast_iterator &operator++() { B::operator++(); return *this; }
     dynamic_cast_iterator &operator--() { B::operator--(); return *this; }

   private:
     static T doCast(const F val) { return dynamic_cast<T>(val); }
   };


Boost list run by bdawes at acm.org, gregod at cs.rpi.edu, cpdaniel at pacbell.net, john at johnmaddock.co.uk