Iterator "convertable to" problem
chris jefferson
caj@cs.york.ac.uk
Sat Sep 18 09:07:00 GMT 2004
Paolo Carlini wrote:
> Hi Chris,
>
>> However, on a related note, do you have any comments on if we have to
>> start static_casting lots of things :)
>
>
> Probably, people reading the list would appreciate a testcase
> demonstrating the problem in a practical way...
While I've now decided I was perhaps over-reading the standard, I shall
provide an example anyway, borrowed from the c++std-lib mailing list.
Our (and almost everyone's) implemention of find for input iterators
looks like this:
template<typename _InputIter, typename _Tp>
inline _InputIter
find(_InputIter __first, _InputIter __last,
const _Tp& __val,
input_iterator_tag)
{
while (__first != __last && !(*__first == __val))
++__first;
return __first;
}
The problem comes from the fact that as __first and __last are input
iterators, then strictly "__first != __last" does not have to be bool.
It only has to be convertable to bool. We could therefore write an
overload of && for the actual type of __first != __last, which would
totally mess up the short-cutting that we are assuming will happen (as
obviously we only want to try !(*__first == __val) when __first!=__last).
However, this kind of pendanticness seems to be a little unnessasary :)
>
> My first instinctive reaction (I haven't really studied the issue) is
> that hardly the designers of the C++ runtime library would have liked
> implementations filled of static_casts... therefore, if the text
> *really* needs that for strict conformance, then probably the text
> should be tweaked, not the implementations... ;)
>
> Paolo.
More information about the Libstdc++
mailing list