From -8392172489777817893
X-Google-Thread: 7894ca11fe,e1cf64e55daed60f,start
X-Google-Attributes: gid7894ca11fe,public,usenet
X-Google-NewGroupId: yes
X-Google-Language: ENGLISH,ASCII-7-bit
Received: by 10.180.86.201 with SMTP id r9mr2929974wiz.4.1355751868172;
        Mon, 17 Dec 2012 05:44:28 -0800 (PST)
MIME-Version: 1.0
Path: l12ni127702wiv.1!nntp.google.com!npeer03.iad.highwinds-media.com!news.highwinds-media.com!feed-me.highwinds-media.com!news.astraweb.com!border2.newsrouter.astraweb.com!newspeer1.nac.net!news.mi.ras.ru!goblin2!goblin.stu.neva.ru!feeder.erje.net!eu.feeder.erje.net!news.albasani.net!.POSTED!not-for-mail
From: ricilake@googlemail.com
Newsgroups: comp.std.c++
Subject: Can a striding iterator adaptor be a forward iterator?
Date: Fri, 14 Dec 2012 09:20:08 -0800 (PST)
Organization: unknown
Lines: 36
Sender: std-cpp-request@vandevoorde.com
Approved: stephen.clamage@oracle.com
Message-ID: <7dd38eec-6df6-4415-b900-620a67595ef5@googlegroups.com>
NNTP-Posting-Host: JkwlTqtdwe0x9h9QgTWxKeb/dqKvJ51qd/3pGqODzcs=
X-Trace: news.albasani.net owl3yD9iCs2zLg3DA4160rq0vodP0hRWtZ0d+Lz8fmb1zekCQlI+TepeeRdevoLMvtZ2hkgbcneSvrS5vr07/A==
X-Complaints-To: abuse@albasani.net
NNTP-Posting-Date: Fri, 14 Dec 2012 17:19:45 +0000 (UTC)
X-Mailer: Perl5 Mail::Internet v2.05
X-Submission-Address: std-cpp-submit@vandevoorde.com
Cancel-Lock: sha1:pZP+z17CSDpYGlk20W/PSG69lOo=
X-Original-Date: Wed, 12 Dec 2012 14:53:16 -0800 (PST)
X-Received-Bytes: 3175
Content-Type: text/plain; charset=ISO-8859-1

I think there is an example of a striding iterator adaptor in "C++ Cookbook" (Cogswell, Diggins&  Stephens, 2006, O'Reilly) but the idea is simple enough: I create a striding adaptor with something like:

     striding_iterator<typename decltype(c)::iterator>  every_third(c.begin(), 3);

and then I use it, as indicated, to reference every third element. (Some mechanics are necessary in order to create an end iterator, but it's feasible enough and that's not my question.)

The problem comes when I create another one:

     striding_iterator<typename decltype(c)::iterator>  every_fifth(c.begin(), 6);

Now I advance the first one five times and the second one three times, and compare them for equality. They both reference the same underlying element, so according to 24.2.5 paragraph(6):

>  If a and b are both dereferenceable, then a == b if and only if *a and *b are bound to the same object

So they should compare equal, which is what I did. (The Cookbook implementation asserts, which seems wrong to me.)

However, I just noticed 24.2.5 paragraph(3) which defines the "multi-pass guarantee" which requires that:

>  a == b implies ++a == ++b

However, that's certainly not true of the striding iterators, unless the stride is the same.

And 24.2.5 paragraph(1) insists that X is a forward iterator only if

>  objects of type X offer the multi-pass guarantee,

Now, am I incorrect to tag this adaptor as a forward_iterator (or bidirectional_iterator, if the underlying iterator is bidirectional)? Is that an intentional prohibition?

Clearly I could create a templated collection of striding adaptors with compile-time fixed strides, which would not trigger this problem. However, restricting the strides to compile-time expressions would not fit my use case.


-- 
[ comp.std.c++ is moderated.  To submit articles, try posting with your ]
[ newsreader.  If that fails, use mailto:std-cpp-submit@vandevoorde.com ]
[              --- Please see the FAQ before posting. ---               ]
[ FAQ: http://www.comeaucomputing.com/csc/faq.html                      ]


