public inbox for gcc-help@gcc.gnu.org
 help / color / mirror / Atom feed
* STL iterators in gcc 3.2
@ 2002-10-25  8:24 Alberto Garcia Raboso
  2002-10-25  8:42 ` John Love-Jensen
  0 siblings, 1 reply; 4+ messages in thread
From: Alberto Garcia Raboso @ 2002-10-25  8:24 UTC (permalink / raw)
  To: gcc-help

I have just changed from Debian GNU/Linux to Gentoo GNU/Linux, which comes
with gcc 3.2.

I have tried to compile some programs that worked fine under gcc 2.95.x
and 3.1, but now I got some problems with them:

1.- Whereas I didn't need it before, now I have to use the "using
namespace std" directive if I want to use cout, cin,... and all this
stuff. Why the change?

2.- This is more serious. In my programs I use a lot of STL vectors, and
so, iterators too. When I had a method with a pointer argument, I passed a
random iterator to it. It worked fine with previous versions of gcc, but
not with 3.2. Is there any way to get it work or do I have to come back to
an older version?

Thanks in advance,

~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
Alberto Garcia Raboso
CERN / EP
CH-1211 Geneva 23, Switzerland
Office 40-2B-19 (+41 22 76 71626)
E-Mail: Alberto.Garcia.Raboso@cern.ch
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
Well, farewell, my hobbits! You should come safe to your own
homes now, and I shall not be kept awake for fear of your
peril. We will send word when we may, and some of us may yet
meet at times; but I fear that we shall not all be gathered
together ever again.

                      The return of the King. J.R.R. Tolkien
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

^ permalink raw reply	[flat|nested] 4+ messages in thread

* Re: STL iterators in gcc 3.2
  2002-10-25  8:24 STL iterators in gcc 3.2 Alberto Garcia Raboso
@ 2002-10-25  8:42 ` John Love-Jensen
  0 siblings, 0 replies; 4+ messages in thread
From: John Love-Jensen @ 2002-10-25  8:42 UTC (permalink / raw)
  To: Alberto Garcia Raboso, gcc-help

Hi Alberto,

> 1.- Whereas I didn't need it before, now I have to use the "using
> namespace std" directive if I want to use cout, cin,... and all this
> stuff. Why the change?

Compliance with ISO 14882 specification standard for C++.

> 2.- This is more serious. In my programs I use a lot of STL vectors, and
> so, iterators too. When I had a method with a pointer argument, I passed a
> random iterator to it. It worked fine with previous versions of gcc, but
> not with 3.2. Is there any way to get it work or do I have to come back to
> an older version?

An iterator is not, necessarily, a pointer.  It could be -- and often is --
a UDT.

Can you use template methods?  (Would that be Generic Programming, of a
sort?)

Do you have a toy example?

This...
void Foo(int* begin, int* end);
...is different from this...
void Foo(vector<int>::iterator begin, vector<int>::iterator end);

--Eljay

^ permalink raw reply	[flat|nested] 4+ messages in thread

* RE: STL iterators in gcc 3.2
  2002-10-25  8:43 Moore, Mathew L
@ 2002-10-25  8:57 ` Alberto Garcia Raboso
  0 siblings, 0 replies; 4+ messages in thread
From: Alberto Garcia Raboso @ 2002-10-25  8:57 UTC (permalink / raw)
  To: Moore, Mathew L; +Cc: 'Alberto Garcia Raboso', gcc-help


> 
> From what I've noticed, the old 2.95 implemented std::vector iterators as
> just plain pointers (|std::vector<double>| used |double*|'s for iteration).
> It looks like now the library is using class abstractions for its iterators.
> This has many benefits, but it also means that some code, e.g.,
> 
> 	void foo(double*);
> 	std::vector<double> myvect;
> 	foo(myvect.begin());
> 
> will no longer work, since |myvect.begin()| does not necessarily produce a
> double*.  
> 
> I have found in order to make your code independent of the iterator object,
> you have to either use templates,
> 
> 	template <typename OutputIterator>
> 	void foo(OutputIterator);
> 
> or you must specifically use the iterator type defined by your container,
> 
> 	void foo(std::vector<double>::iterator);
> 
> I don't know if there is an easier conversion than either of these, but I
> would be interested in hearing any other ideas.

Of course, this explains vey well he problem. But I cannot use new
definitions of the methods using template arguments instead of pointers,
as they come from a library... So I think I'll have to use a previous
version of gcc, at least until I have time to change my code to adhere to
the standards ;-)

Anyway, thank you very much.

~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
Alberto Garcia Raboso
CERN / EP
CH-1211 Geneva 23, Switzerland
Office 40-2B-19 (+41 22 76 71626)
E-Mail: Alberto.Garcia.Raboso@cern.ch
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
Well, farewell, my hobbits! You should come safe to your own
homes now, and I shall not be kept awake for fear of your
peril. We will send word when we may, and some of us may yet
meet at times; but I fear that we shall not all be gathered
together ever again.

                      The return of the King. J.R.R. Tolkien
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

^ permalink raw reply	[flat|nested] 4+ messages in thread

* RE: STL iterators in gcc 3.2
@ 2002-10-25  8:43 Moore, Mathew L
  2002-10-25  8:57 ` Alberto Garcia Raboso
  0 siblings, 1 reply; 4+ messages in thread
From: Moore, Mathew L @ 2002-10-25  8:43 UTC (permalink / raw)
  To: 'Alberto Garcia Raboso', gcc-help

> 
> 1.- Whereas I didn't need it before, now I have to use the "using
> namespace std" directive if I want to use cout, cin,... and all this
> stuff. Why the change?
>


This version of gcc (and g++) is more standards compliant, hence the new
necessity of std:: when using the iostreams and other standard library
features.

 
> 2.- This is more serious. In my programs I use a lot of STL 
> vectors, and
> so, iterators too. When I had a method with a pointer 
> argument, I passed a
> random iterator to it. It worked fine with previous versions 
> of gcc, but
> not with 3.2. Is there any way to get it work or do I have to 
> come back to
> an older version?
> 


From what I've noticed, the old 2.95 implemented std::vector iterators as
just plain pointers (|std::vector<double>| used |double*|'s for iteration).
It looks like now the library is using class abstractions for its iterators.
This has many benefits, but it also means that some code, e.g.,

	void foo(double*);
	std::vector<double> myvect;
	foo(myvect.begin());

will no longer work, since |myvect.begin()| does not necessarily produce a
double*.  

I have found in order to make your code independent of the iterator object,
you have to either use templates,

	template <typename OutputIterator>
	void foo(OutputIterator);

or you must specifically use the iterator type defined by your container,

	void foo(std::vector<double>::iterator);

I don't know if there is an easier conversion than either of these, but I
would be interested in hearing any other ideas.

--Matt

^ permalink raw reply	[flat|nested] 4+ messages in thread

end of thread, other threads:[~2002-10-25 15:57 UTC | newest]

Thread overview: 4+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2002-10-25  8:24 STL iterators in gcc 3.2 Alberto Garcia Raboso
2002-10-25  8:42 ` John Love-Jensen
2002-10-25  8:43 Moore, Mathew L
2002-10-25  8:57 ` Alberto Garcia Raboso

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).