assigning reverse_iterator to const_reverse_iterator

S

subramanian100in

consider the program x.cpp:

#include <cstdlib>
#include <iostream>
#include <string>
#include <list>
#include <algorithm>

using namespace std;

int main()
{
string str("test string");

typedef list<char> Container;

Container c(str.begin(), str.end());

Container::const_iterator cit = find(c.begin(), c.end(), 's');

Container::const_reverse_iterator crit = find(c.rbegin(),
c.rend(), 's');

return EXIT_SUCCESS;
}

I compiled it with g++3.4.3 as
g++ -std=c++98 -pedantic -Wall -Wextra x.cpp

It compiles fine.

In the line
Container::const_iterator cit = find(c.begin(), c.end(), 's');
I am assigning plain iterator to const_iterator. The ISO/IEC
14882:2003 document mentions the following in section 23.1 - Container
Requirements in the table 65.

For X::iterator, the assertion/note/pre/post-condition column says
that 'convertible to X::const_iterator'.

Now consider the line
Container::const_reverse_iterator crit = find(c.rbegin(),
c.rend(), 's');
Here I am assigning reverse_iterator to const_reverse_iterator. In ISO/
IEC 14882:2003 document, in Table 66 - 'Reversible container
requirements' the following is mentioned:
For X::reverse_iterator, the assertion/note/pre/post-condition column
says
reverse_iterator <iterator>. It doesn't say 'convertible to
X::const_reverse_iterator'.
Is it implied here ? Or will all implementations allow assignment of
reverse_iterator to const_reverse_iterator ?

Kindly clarify.

Thanks
V.Subramanian
 
J

James Kanze

consider the program x.cpp:
#include <cstdlib>
#include <iostream>
#include <string>
#include <list>
#include <algorithm>
using namespace std;
int main()
{
string str("test string");
typedef list<char> Container;
Container c(str.begin(), str.end());
Container::const_iterator cit = find(c.begin(), c.end(), 's');
Container::const_reverse_iterator crit = find(c.rbegin(),
c.rend(), 's');
return EXIT_SUCCESS;
}
I compiled it with g++3.4.3 as
g++ -std=c++98 -pedantic -Wall -Wextra x.cpp
It compiles fine.
In the line
Container::const_iterator cit = find(c.begin(), c.end(), 's');
I am assigning plain iterator to const_iterator. The ISO/IEC
14882:2003 document mentions the following in section 23.1 -
Container Requirements in the table 65.
For X::iterator, the assertion/note/pre/post-condition column says
that 'convertible to X::const_iterator'.
Now consider the line
Container::const_reverse_iterator crit = find(c.rbegin(),
c.rend(), 's');
Here I am assigning reverse_iterator to const_reverse_iterator. In ISO/
IEC 14882:2003 document, in Table 66 - 'Reversible container
requirements' the following is mentioned:
For X::reverse_iterator, the assertion/note/pre/post-condition
column says
reverse_iterator <iterator>. It doesn't say 'convertible to
X::const_reverse_iterator'.
Is it implied here ? Or will all implementations allow
assignment of reverse_iterator to const_reverse_iterator ?

Table 66 says that reverse_iterator must be a typedef for
std::reverse_iterator< iterator >, and that
const_reverse_iterator must be a typedef for
std::reverse_iterator< const_iterator >. And the template class
reverse_iterator has a converting constructor which will convert
reverse_iterator< U > to reverse_iterator< T > anytime U is
convertable to T.

The standard doesn't say much about reverse_iterator's of the
containers, because it doesn't have to---they're required to be
instances of std::reverse_iterator, which says all that is
needed.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,990
Messages
2,570,211
Members
46,796
Latest member
SteveBreed

Latest Threads

Top