473,402 Members | 2,050 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,402 software developers and data experts.

Implicit conversion of iterator to reverse_iterator

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::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.

Apr 30 '07 #1
3 2871
bb wrote:
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.
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.

--
Markus

Apr 30 '07 #2
On 30 Apr, 10:27, bb <muralibal...@gmail.comwrote:
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?

Apr 30 '07 #3
On 30 Apr., 16:03, dasjotre <dasjo...@googlemail.comwrote:
On 30 Apr, 10:27, bb <muralibal...@gmail.comwrote:


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?
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

Apr 30 '07 #4

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

2
by: Martin Magnusson | last post by:
Hi I'm traversing a std::list backwards with a reverse_iterator, and need to check for the case when the iterator points to the first element of the list. My code now looks something like the...
8
by: Alexander Stippler | last post by:
Hello, the standard requires a type reverse_iterator. But in the form it is required and implemented for all compilers I know, there is IMO something missing, which is essential for its usage....
5
by: AlesD | last post by:
Hello, could you please help me with following problem: I have reversible container which holds (unsorted) instances of objects with common base class. From time to time I need to search for...
29
by: Hagen | last post by:
Hello, in a recent thread "speed of vector vs array" I read about the problem of the slow acces by addressing vector elements by indexing, unfortunately I see no workaround in my case. My...
0
by: nick | last post by:
Hi, I need to manage a "layered" collection of objects, where each layer grows independently, e.g, o--+--+--+--+--+ 1st layer | o 2nd layer (empty) | o--+--+--+ 3rd...
13
by: jason | last post by:
My question is regaring STL iterator/reverse_iterator. I would like to write a function which does the following int CFoo::calculate(std::vector<int>::iterator itBegin,...
4
by: KL | last post by:
Well, I got through those other hurdles, but now I am stuck with trying to get an iterator to work. I am having a problem with trying to iterate through the STL list container that I set up. I...
1
by: tim | last post by:
Our coding standard checker is reporting an implicit user conversion for the following: === std::vector<PepLines>* ptr = NULL; std::map<peppage::ePepPage, std::vector<PepLines>*>::iterator...
3
by: Dalbosco J-F | last post by:
Hi, Sorry if this has already been answered. Given a std::list and a reverse_iterator is there a way to erase the element pointed to by the reverse_iterator via the erase method? Apparently...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.