Boost logo

Boost :

Subject: Re: [boost] [iterator] UB when implicitly using default constructed counting_iterator<unsigned>
From: Steven Watanabe (watanabesj_at_[hidden])
Date: 2012-12-06 12:10:39


AMDG

On 12/06/2012 12:10 AM, "Claas H. Köhler" wrote:
>
> Hi John. You are correct. The following code returns false/0 (with gcc-4.7):
>
> typedef std::array<double, 10>::iterator T;
> T ita1, ita2;
>
> std::cout << (ita1 == ita2) <<std::endl;
>
> However, you can tweak it like this:
>
> typedef std::array<double, 10>::iterator T;
> T ita1=T(), ita2=T();
>
> std::cout << (ita1 == ita2) <<std::endl;
>
> and it returns true /1. It is mainly the lack of this functionality which is on my wish-list for the
> counting_iterator.
>

This is still undefined behavior.

"Iterators can also have singular values that are not
associated with any sequence. ... Results of most expressions
are undefined for singular values; the only exceptions
are destroying an iterator that holds a singular value,
the assignment of a non-singular value to an iterator
that hold a singular value, and, for iterators that
satisfy the DefaultConstructible requirements, using
a value-initialized iterator as the source of a copy
or move operation." (C++11 24.2.1)

In Christ,
Steven Watanabe


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