Re: Iterators: distance versus operator- argument types.

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

 



rgamarra <rgamarra@xxxxxxxxx> writes:

> int
> main()
> {
>   std::vector<int>::const_iterator i;
>   std::vector<int>::iterator j;
>   i - j;
>   // std::distance(i, j); // not the same iterator types
> }

> Now, the call to operator- succeeds. Is it legal standard-C++?
> Particularly, is that mixing of types in the expression required to be
> possible by the standard? Or is it an gnu-extension? I'm using g++
> (Ubuntu 4.3.3-5ubuntu4) 4.3.3.

There is a conversion from iterator to const_iterator.  So in this case
j is converted to const_iterator in order to make the types the same.

> Now in the sources, I see that (unlike distance) operator- is defined
> with two different template type parameters, which, I understand,
> allow to perform, directly, that mixing.

Specifically to permit mixing iterator and const_iterator, yes.

Ian


[Index of Archives]     [Linux C Programming]     [Linux Kernel]     [eCos]     [Fedora Development]     [Fedora Announce]     [Autoconf]     [The DWARVES Debugging Tools]     [Yosemite Campsites]     [Yosemite News]     [Linux GCC]

  Powered by Linux