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-03 15:24:48


AMDG

On 12/03/2012 06:07 AM, "Claas H. Köhler" wrote:
>
> Consider e.g. a simple template of the form
>
> template<class IT>
> struct Range {
> IT first, last;
>
> Range(void)= default;
>
> size_t size(void) const {return last - first;}
> };
>

I do not know of any iterator for which

Range<II> r;
std::size_t size = r.size();

is legal.

> The behaviour of size() is currently undefined for default constructed Range objects, which is a
> major disadvantage in my opinion, since any reproducible default value will result in the expected
> behaviour of size zero for a default constructed Range object. This is what all other iterator
> implementations guarantee, too, if I am not mistaken (Please correct me if I am wrong here).
>

Most iterators do not make this guarantee:

std::vector/list/string/deque/map/set<T>::iterator
   - default constructed iterators may be singular.
     value initialization may not make any difference.
T* - default constructed iterator is uninitialized
std::istream_iterator - okay, end
std::istreambuf_iterator - okay, end

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