Boost logo

Boost :

Subject: Re: [boost] [iterator] UB when implicitly using default constructed counting_iterator<unsigned>
From: Claas H. Köhler (claas.koehler_at_[hidden])
Date: 2012-12-04 10:36:42


On 03/12/12 21:24, Steven Watanabe wrote:
> 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
On my machine (gcc-4.7 with linux) the iterators of vector, list, set and string are all initialised
to something equivalent to zero. T* can be forced to obtain a reproducible value by writing

typedef T* X;

X x= X(),

which will also result in well defined behaviour when default constructing an element, which is
highly desirable in my opinion. What is your suggestion to achieve something similar with the current
implementation of couting_iterator? What are the drawbacks of Peter's suggestion from your point of view?

Regards
Claas

>
> In Christ,
> Steven Watanabe
>
> _______________________________________________
> Unsubscribe & other changes: http://lists.boost.org/mailman/listinfo.cgi/boost
>

-- 
Deutsches Zentrum für Luft- und Raumfahrt e.V. (DLR)
Institut für Methodik der Fernerkundung | Experimentelle Verfahren | Münchner Str | 82234 Weßling
Claas H. Köhler
Telefon 08153 28-1274 | Telefax 08153 28-1337 | claas.koehler_at_[hidden]
www.DLR.de/EOC

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