Re: Iterators: distance versus operator- argument types.

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

 



Hi Ian,

First of all, thanks a lot for your answer and help about this.

I read what you pointed and, I believe, cannot fully grasp it. First,

> There is a conversion from iterator to const_iterator.

which makes me think that, because of that conversion, there would be
no need of the flexibility given by using two different template
parameters.

Then,

> Specifically to permit mixing iterator and const_iterator, yes.

but, conversely, if two different template type parameters are
allowed, no conversion will happen. I believe that each one will bind
to the declared type of the variable.

To check and see what's going on, I did

g++ -E main.cc

, got the full sources and modified the operator- by adding (with the
proper #include <typeinfo>)

	std::cout << typeid(_IteratorL).name() << std::endl;
        std::cout << typeid(_IteratorR).name() << std::endl;

Running, I get the following

PKi
Pi

which, I think, stands for int const* and int*.

So it seems that each iterator type stays as declared, because the
output corresponds, respectively, to the types used in the definition
of const_iterator and iterator.

      typedef __gnu_cxx::__normal_iterator<pointer, vector_type> iterator;
      typedef __gnu_cxx::__normal_iterator<const_pointer, vector_type>
      const_iterator;

where

      typedef const _Tp* const_pointer;
      typedef _Tp* pointer;

Because of this, I understand that there's no conversion, and so (as
the standard is only requiring that "a - b" is legal when their type
is the same) have some doubts about the portability of the expression
(in the sense of being guaranteed to work by the standard).

Cheers, and thanks a lot one more time.

--
Rodolfo Federico Gamarra



On Tue, Aug 24, 2010 at 11:25, Ian Lance Taylor <iant@xxxxxxxxxx> wrote:
> 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