Boost logo

Boost :

From: Andrey Semashev (andysem_at_[hidden])
Date: 2007-05-12 07:45:33


Hello David,

Thursday, May 10, 2007, 2:49:56 PM, you wrote:

> on Wed May 09 2007, Andrey Semashev <andysem-AT-mail.ru> wrote:

>> Hello David,
>>
>> Wednesday, May 9, 2007, 8:27:53 PM, you wrote:
>>
>>> on Tue May 08 2007, Andrey Semashev <andysem-AT-mail.ru> wrote:
>>
>>>> Hello,
>>>>
>>>> Quite often I find myself writing an is_const_iterator trait which
>>>> looks like this:
>>
>> [snip]
>>
>>>> Maybe it is worth adding to Boost.Iterators library? Or maybe there
>>>> already is something like that?
>>
>>> What's it useful for?
>>
>> Mostly I used it to implement my iterators for some adopted STL-like
>> containers. It shortens the iterator_facade template parameters list:
>>
>> template< NodeItT >
>> struct MyIterator :
>> public iterator_facade<
>> MyIterator< NodeItT >,
>> typename mpl::if_<
>> is_const_iterator< NodeItT >,
>> const value_type,
>> value_type
>> >::type,
>> typename iterator_category< NodeItT >::type
>> >
>> {
>> };

> Is there some good reason you're not using iterator_adaptor here?
> This looks like a classic use case for it. That would eliminate the
> need for is_const_iterator in this case.

I apologize for returning to this, but as I'm reading the docs and
looking at my use cases I have before me I don't see how would I elide
is_const_iterator when using iterator_adaptor. Suppose this:

  template< typename ValueT >
  class MyList
  {
  public:
    typedef ValueT value_type;

    // etc. all other common typedefs except iterators

  private:
    struct MyNode
    {
      ValueT value;
      int m_SomeOtherData;
    };
    typedef std::list< MyNode > underlying_container;

    template< typename >
    class MyIterator;

  public:
    typedef MyIterator<
      typename underlying_container::iterator
> iterator;

    typedef MyIterator<
      typename underlying_container::const_iterator
> const_iterator;

  private:
    template< typename ItT >
    class MyIterator :
      public iterator_adaptor<
        MyIterator< ItT >,
        ItT,
        value_type, // I have to substitute value type
        use_default, // Category fits well
        ??? // What should I write for reference type?
             // I've provided value type, so value_type&
             // is not valid when ItT is const_iterator
>
    {
      typedef typename iterator_adaptor<
        ...
>::reference reference;

      reference dereference()
      {
        return this->base()->value;
      }
    };
  };

So, once again I have to detect ItT constness and form up the correct
reference type for the iterator_adaptor. Am I missing something?
  

-- 
Best regards,
 Andrey                            mailto:andysem_at_[hidden]

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