Connecting Tech Pros Worldwide Forums | Help | Site Map

Implicit conversion of iterator to reverse_iterator

bb
Guest
 
Posts: n/a
#1: Apr 30 '07
Hi,
Please could you clarify why 'implicit conversion' does not take place
while assigning an iterator to reverse_iterator. However, it happens
while initializing/constructing.

e.g.

typedef std::map<int, std::stringMIS;
MIS m1;

// populating m1

MIS::iterator it = m1.lower_bound(4);

// checking the validity of 'it' etc.

MIS::reverse_iterator rit1(it); // implicit conversion takes place
as expected
MIS::reverse_iterator rit2 = it; // does not compile! why there is
no implicit conversion here?
MIS::reverse_iterator rit3 =
static_cast<MIS::reverse_iterator>(it); // compiles & all looks
fine. Is it legal?

Thanks.


Markus Schoder
Guest
 
Posts: n/a
#2: Apr 30 '07

re: Implicit conversion of iterator to reverse_iterator


bb wrote:
Quote:
Hi,
Please could you clarify why 'implicit conversion' does not take place
while assigning an iterator to reverse_iterator. However, it happens
while initializing/constructing.
>
e.g.
>
typedef std::map<int, std::stringMIS;
MIS m1;
>
// populating m1
>
MIS::iterator it = m1.lower_bound(4);
>
// checking the validity of 'it' etc.
>
MIS::reverse_iterator rit1(it); // implicit conversion takes place
as expected
This calls the constructor reverse_iterator(Iterator x) explicitly -- no
conversion involved.
Quote:
MIS::reverse_iterator rit2 = it; // does not compile! why there is
no implicit conversion here?
Because the constructor reverse_iterator(Iterator x) is declared
explicit.
Quote:
MIS::reverse_iterator rit3 =
static_cast<MIS::reverse_iterator>(it); // compiles & all looks
fine. Is it legal?
This is legal and does work.

--
Markus

dasjotre
Guest
 
Posts: n/a
#3: Apr 30 '07

re: Implicit conversion of iterator to reverse_iterator


On 30 Apr, 10:27, bb <muralibal...@gmail.comwrote:
Quote:
Hi,
Please could you clarify why 'implicit conversion' does not take place
while assigning an iterator to reverse_iterator. However, it happens
while initializing/constructing.
>
e.g.
>
typedef std::map<int, std::stringMIS;
MIS m1;
>
// populating m1
>
MIS::iterator it = m1.lower_bound(4);
>
// checking the validity of 'it' etc.
>
MIS::reverse_iterator rit1(it); // implicit conversion takes place
as expected
this is explicit constructor call.

if implicit conversion was allowed
what would you expect compiler to
do with:

for_each(m1.begin(), m1.end(), do_something());
// followed by
for_each(m1.rbegin(), m1.rend(), do_something());

how woudl you expect for_each to make distinction
between reverse_iterator and iterator?

peter koch
Guest
 
Posts: n/a
#4: Apr 30 '07

re: Implicit conversion of iterator to reverse_iterator


On 30 Apr., 16:03, dasjotre <dasjo...@googlemail.comwrote:
Quote:
On 30 Apr, 10:27, bb <muralibal...@gmail.comwrote:
>
>
>
>
>
Quote:
Hi,
Please could you clarify why 'implicit conversion' does not take place
while assigning an iterator to reverse_iterator. However, it happens
while initializing/constructing.
>
Quote:
e.g.
>
Quote:
typedef std::map<int, std::stringMIS;
MIS m1;
>
Quote:
// populating m1
>
Quote:
MIS::iterator it = m1.lower_bound(4);
>
Quote:
// checking the validity of 'it' etc.
>
Quote:
MIS::reverse_iterator rit1(it); // implicit conversion takes place
as expected
>
this is explicit constructor call.
>
if implicit conversion was allowed
what would you expect compiler to
do with:
>
for_each(m1.begin(), m1.end(), do_something());
// followed by
for_each(m1.rbegin(), m1.rend(), do_something());
>
how woudl you expect for_each to make distinction
between reverse_iterator and iterator?
I do not see the problem here. An iterator is not a reverse iterator,
and both functions would be "perfect fits". What could be dangerous
would be to mix iterators and reverse_iterators, but in templated code
where no implicit conversions are made, I see no problems.

/Peter

Closed Thread


Similar C / C++ bytes