ramashishb@gmail.com wrote:
[color=blue][color=green][color=darkred]
>> > typename iterator_traits<_InputIterator>::value_type __value =
>> > *__first;
>> > *__result = __value;
>> > while (++__first != __last)
>> > if (!__binary_pred(__value, *__first))
>> > {
>> > __value = *__first;
>> > *++__result = __value;
>> > }
>> >
>> > The offending line is
>> >
>> > __value = *__first;
>> >
>> > in the inner loop which tries to assign to a variable of type
>> > InputIterator::value_type. That fails as its a std::pair<const int,
>> > int> whose *first* is *const int*.[/color]
>>
>> Is this correct, though? It strikes me as an irritating and
>> unnecessary restriction, and while I'm loath to suggest that the gcc
>> STL implementation is wrong, I find nothing more plausible. I believe
>> it's correct for map to store pair<const int, int>. It seems like
>> unique_copy should be able to operate within the constraint of a
>> non-assignable value type, so long as that type is copy-constructible.
>> It may not be as efficient, but that could perhaps be addressed by
>> template specialization. Am I missing something here?[/color]
>
> I too wouldn't like to suggest that the implementation is wrong. But
> its possible for the implementation to be less restrictive (to be able
> to work with non-assignable value type). Instead of saving the values
> one could save the iterator position for doing comparisons. Here's the
> modified code (original commented out)-
>[/color]
[color=blue]
> _InputIterator __previous = __first;
> *__result = *__previous;
> while (++__first != __last) {
> if (!__binary_pred(*__previous, *__first))[/color]
This will *not* work: __previous is invalidated by incrementing __first. The
postconditions for ++r on input iterator are stated as follows [24, Table
72]:
++r X&
pre: r is dereferenceable.
post: r is dereferenceable or r is past-the-end.
post: any copies of the previous value of r are no longer
required either to be dereferenceable or to be in the domain of ==
[color=blue]
> {
> __previous = __first;
> *++__result = *__previous;
> }
> }
>
> That puts the requirement of _InputIterator to be assignable though.
> Not sure whether this might be a restriction.[/color]
No, input iterators are assignable. However, you cannot use stored values of
an iterator that has been incremented for dereferencing.
Best
Kai-Uwe Bux