Topic: incrementing container<T>::begin()


Author: questionguy@my-deja.com
Date: 2000/03/17
Raw View

int main ()
  {
    std::vector<int> vi (10);
    std::vector<int>::iterator it = ++vi.begin();
  }

when I do this on VC6 or BCB4 I get errors
that say an l-value is required (for '++').  I am also
told that this code produces similar errors
on g++.

If 3 compilers have consistant results, I assume
they are right but it is not clear to me
why the above code will not compile.

It is my understanding that begin ()
will return a reference to the iterator,
why cant I call pre-increment operator on it ?


Sent via Deja.com http://www.deja.com/
Before you buy.

---
[ comp.std.c++ is moderated.  To submit articles, try just posting with ]
[ your news-reader.  If that fails, use mailto:std-c++@ncar.ucar.edu    ]
[              --- Please see the FAQ before posting. ---               ]
[ FAQ: http://reality.sgi.com/austern_mti/std-c++/faq.html              ]






Author: Francis Glassborow <francis@robinton.demon.co.uk>
Date: 2000/03/18
Raw View
In article <8aq2no$2qd$1@nnrp1.deja.com>, questionguy@my-deja.com writes
>It is my understanding that begin ()
>will return a reference to the iterator,
>why cant I call pre-increment operator on it ?
>
No, I think you will find that it returns an iterator by value.  Even if
it did not it should return by const reference because it would be
fundamentally wrong to allow user code to corrupt the iterator pointing
to the beginning of a container.

It is true that in some circumstances C++ allows you to call member
functions on returned objects (by value) which is why we now have to
carefully return by const value if we want to limit this to const member
functions. However it is quite likely that the iterator type will be T*
for a vector.

Francis Glassborow      Journal Editor, Association of C & C++ Users
64 Southfield Rd
Oxford OX4 1PA          +44(0)1865 246490
All opinions are mine and do not represent those of any organisation

---
[ comp.std.c++ is moderated.  To submit articles, try just posting with ]
[ your news-reader.  If that fails, use mailto:std-c++@ncar.ucar.edu    ]
[              --- Please see the FAQ before posting. ---               ]
[ FAQ: http://reality.sgi.com/austern_mti/std-c++/faq.html              ]






Author: Huebner Rainer <477hzd@z.zgs.de>
Date: 2000/03/21
Raw View
questionguy@my-deja.com wrote:

> int main ()
>   {
>     std::vector<int> vi (10);
>     std::vector<int>::iterator it = ++vi.begin();
>   }
>
> when I do this on VC6 or BCB4 I get errors
> that say an l-value is required (for '++').  I am also
> told that this code produces similar errors
> on g++.
>
> If 3 compilers have consistant results, I assume
> they are right but it is not clear to me
> why the above code will not compile.
>
> It is my understanding that begin ()
> will return a reference to the iterator,
> why cant I call pre-increment operator on it ?
>
> Sent via Deja.com http://www.deja.com/
> Before you buy.
>
> ---
> [ comp.std.c++ is moderated.  To submit articles, try just posting with ]
> [ your news-reader.  If that fails, use mailto:std-c++@ncar.ucar.edu    ]
> [              --- Please see the FAQ before posting. ---               ]
> [ FAQ: http://reality.sgi.com/austern_mti/std-c++/faq.html              ]

int main ()
  {
    std::vector<int> vi (10);
    std::vector<int>::iterator it1 = vi.begin() + 1;
  }

Force compiler to use internal instance,  but not as default else a simple
code would translated int a = 10  to   int a = (int xxx 10, a = xxx).




---
[ comp.std.c++ is moderated.  To submit articles, try just posting with ]
[ your news-reader.  If that fails, use mailto:std-c++@ncar.ucar.edu    ]
[              --- Please see the FAQ before posting. ---               ]
[ FAQ: http://reality.sgi.com/austern_mti/std-c++/faq.html              ]






Author: mauricefxxx@ix.netcom.com (Maurice Fox)
Date: 2000/03/23
Raw View
You can't do that because it's wrong.  I don't have the inclination to
type out all of why it's wrong.  What you can do is

std::vector<int>::iterator it - vi.begin();
it++;

HTH - Maurice

On Sun, 17 Mar 3900 22:11:12, questionguy@my-deja.com wrote:

>
>
> int main ()
>   {
>     std::vector<int> vi (10);
>     std::vector<int>::iterator it = ++vi.begin();
>   }
>
> when I do this on VC6 or BCB4 I get errors
> that say an l-value is required (for '++').  I am also
> told that this code produces similar errors
> on g++.
>
> If 3 compilers have consistant results, I assume
> they are right but it is not clear to me
> why the above code will not compile.
>
> It is my understanding that begin ()
> will return a reference to the iterator,
> why cant I call pre-increment operator on it ?
>
>
> Sent via Deja.com http://www.deja.com/
> Before you buy.
>
> ---
> [ comp.std.c++ is moderated.  To submit articles, try just posting with ]
> [ your news-reader.  If that fails, use mailto:std-c++@ncar.ucar.edu    ]
> [              --- Please see the FAQ before posting. ---               ]
> [ FAQ: http://reality.sgi.com/austern_mti/std-c++/faq.html              ]
>


---
[ comp.std.c++ is moderated.  To submit articles, try just posting with ]
[ your news-reader.  If that fails, use mailto:std-c++@ncar.ucar.edu    ]
[              --- Please see the FAQ before posting. ---               ]
[ FAQ: http://reality.sgi.com/austern_mti/std-c++/faq.html              ]