Implicit conversion of iterator to reverse_iterator

B

bb

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::string> MIS;
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.
 
M

Markus Schoder

bb said:
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::string> MIS;
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.
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.
MIS::reverse_iterator rit3 =
static_cast<MIS::reverse_iterator>(it); // compiles & all looks
fine. Is it legal?

This is legal and does work.
 
D

dasjotre

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::string> MIS;
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?
 
P

peter koch

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
 

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

Forum statistics

Threads
473,767
Messages
2,569,572
Members
45,046
Latest member
Gavizuho

Latest Threads

Top