implementation of rbegin() const in g++ 3.4.3

S

subramanian100in

I want to understand how 'rbegin() const' returns a
std::const_reverse_iterator.

Kindly go through the following and correct me where-ever I am wrong.

I am using g++ 3.4.3. In this implementation, 'rbegin() const' is
defined as
const_reverse_iterator
rbegin() const { return const_reverse_iterator(end()); }

Here 'rbegin() const' returns std::const_reverse_iterator(end())
which is the same as std::reverse_iterator<const_iterator>(end()). In
this, the return type of end() is 'iterator'. So we essentially have
std::reverse_iterator<const_iterator>(iterator). This has to invoke
the following converting ctor in reverse_iterator class template:

template <class U>
reverse_iterator(const reverse_iterator<U>& u);

But for this to take place, the RHS argument in
std::reverse_iterator<const_iterator>(iterator) must be
reverse_iterator<iterator>. Since we only have 'iterator', it must be
converted to reverse_iterator<iterator> using the ctor

explicit reverse_iterator(Iterator x);

But, as mentioned, this ctor is declared 'explicit'. So I am unable to
understand how the implicit conversion of 'iterator' in
std::reverse_iterator<const_iterator>(iterator) to
reverse_iterator<iterator> can happen.

we have:
std::reverse_iterator<const_iterator>(iterator)
This has to be converted to:
std::reverse_iterator<const_iterator>(
reverse_iterator<iterator>);
Then only the latter can invoke the converting ctor in
reverse_iterator class template. Is this understanding of mine is
correct ?

Kindly clarify.

Thanks
V.Subramanian
 
V

Victor Bazarov

I want to understand how 'rbegin() const' returns a
std::const_reverse_iterator.

Kindly go through the following and correct me where-ever I am wrong.

I am using g++ 3.4.3. In this implementation, 'rbegin() const' is
defined as
const_reverse_iterator
rbegin() const { return const_reverse_iterator(end()); }

Here 'rbegin() const' returns std::const_reverse_iterator(end())
which is the same as std::reverse_iterator<const_iterator>(end()). In
this, the return type of end() is 'iterator'. So we essentially have
std::reverse_iterator<const_iterator>(iterator). This has to invoke
the following converting ctor in reverse_iterator class template:

template<class U>
reverse_iterator(const reverse_iterator<U>& u);

But for this to take place, the RHS argument in
std::reverse_iterator<const_iterator>(iterator) must be
reverse_iterator<iterator>. Since we only have 'iterator', it must be
converted to reverse_iterator<iterator> using the ctor

explicit reverse_iterator(Iterator x);

But, as mentioned, this ctor is declared 'explicit'. So I am unable to
understand how the implicit conversion of 'iterator' in
std::reverse_iterator<const_iterator>(iterator) to
reverse_iterator<iterator> can happen.

we have:
std::reverse_iterator<const_iterator>(iterator)
This has to be converted to:
std::reverse_iterator<const_iterator>(
reverse_iterator<iterator>);
Then only the latter can invoke the converting ctor in
reverse_iterator class template. Is this understanding of mine is
correct ?

There are two 'end' functions. One returns 'iterator', and the other
returns 'const_iterator' (IIRC). Review your conclusions based on that.

V
 

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,769
Messages
2,569,582
Members
45,065
Latest member
OrderGreenAcreCBD

Latest Threads

Top