Valhalla Legends Archive

Programming => General Programming => C/C++ Programming => Topic started by: Skywing on November 07, 2004, 10:03 AM

Title: Legal C++ or not?
Post by: Skywing on November 07, 2004, 10:03 AM

#include <list>

int main(int ac, char **av)
{
std::list<int> l;

for(std::list<int>::const_reverse_iterator i = l.rbegin(); i != l.rend(); ++i) ;

return 0;
}


I'm currrently pending a response from Bjarne Stroustrup on this.

It doesn't compile with VC < VC8 beta 1 (e.g. with the Dinkumware STL), Comeau C++ (http://www.comeaucomputing.com/tryitout/) (with the SGI STL), or GCC (with it's default STL).

What do you think?
Title: Re: Legal C++ or not?
Post by: Mephisto on November 07, 2004, 12:51 PM
Quote from: Skywing on November 07, 2004, 10:03 AM

#include <list>

int main(int ac, char **av)
{
std::list<int> l;

for(std::list<int>::const_reverse_iterator i = l.rbegin(); i != l.rend(); ++i) ;

return 0;
}


I'm currrently pending a response from Bjarne Stroustrup on this.

It doesn't compile with VC < VC8 beta 1 (e.g. with the Dinkumware STL), Comeau C++ (http://www.comeaucomputing.com/tryitout/) (with the SGI STL), or GCC (with it's default STL).

What do you think?

The compiler seems to complain about i != l.rend() in terms of not supplying valid operator operands.
Title: Re: Legal C++ or not?
Post by: Skywing on November 07, 2004, 01:43 PM
Quote from: Mephisto on November 07, 2004, 12:51 PM
Quote from: Skywing on November 07, 2004, 10:03 AM

#include <list>

int main(int ac, char **av)
{
std::list<int> l;

for(std::list<int>::const_reverse_iterator i = l.rbegin(); i != l.rend(); ++i) ;

return 0;
}


I'm currrently pending a response from Bjarne Stroustrup on this.

It doesn't compile with VC < VC8 beta 1 (e.g. with the Dinkumware STL), Comeau C++ (http://www.comeaucomputing.com/tryitout/) (with the SGI STL), or GCC (with it's default STL).

What do you think?

The compiler seems to complain about i != l.rend() in terms of not supplying valid operator operands.
I know what the compiler says.  I'm asking if this is legal with a perfectly conforming C++ compiler and a perfectly conforming C++ STL.
Title: Re: Legal C++ or not?
Post by: Mephisto on November 07, 2004, 01:49 PM
It appears to be completely fine/legal.  If you don't use a reverse iterator it compiles (providing you use begin and end not rbegin and rend).  Interesting.
Title: Re: Legal C++ or not?
Post by: dxoigmn on November 07, 2004, 01:54 PM
Why not?


#include <list>

int main(int ac, char **av)
{
std::list<int> l;

for(std::list<int>::reverse_iterator i = l.rbegin(); i != l.rend(); ++i) ;

return 0;
}


Why must i be const_reverse_iterator?
Title: Re: Legal C++ or not?
Post by: Skywing on November 07, 2004, 03:26 PM
Quote from: dxoigmn on November 07, 2004, 01:54 PM
Why not?


#include <list>

int main(int ac, char **av)
{
std::list<int> l;

for(std::list<int>::reverse_iterator i = l.rbegin(); i != l.rend(); ++i) ;

return 0;
}


Why must i be const_reverse_iterator?

The problem is exhibited with const_reverse_iterator.  This is a simple test case to show that.
Title: Re: Legal C++ or not?
Post by: Eibro on November 07, 2004, 04:04 PM
Have you tried with STLport?
Title: Re: Legal C++ or not?
Post by: Skywing on November 07, 2004, 07:22 PM
Quote from: Eibro[yL] on November 07, 2004, 04:04 PM
Have you tried with STLport?
No; I don't have it installed.  BTW, isn't that related to the SGI STL, though (which failed)?
Title: Re: Legal C++ or not?
Post by: Skywing on November 10, 2004, 10:11 AM
Bjarne (http://www.research.att.com/~bs/homepage.html) answers the question:

QuoteThe program is *not* legal. You can convert an iterator to a
const_iterator, but it is not guaranteed that you can convert a
reverse_iterator to a const_reverse_iterator. This is a known problem
that is likely to be corrected in the next revision of the standard. In
the meantime, the implementors are reluctant to provide that conversion
because that would lead to non-protable user code. I don't know of any
good reason why it is not allowed.
Title: Re: Legal C++ or not?
Post by: warz on November 10, 2004, 10:22 PM
I've met Bjarne before when I visited college station. I believe torque has him for computer science.