473,396 Members | 1,917 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,396 software developers and data experts.

Deriving from an iterator?

Is it guaranteed that STL iterators are aggregate types and can thus be
derived from?

I'm thinking along the lines of, for example:

class DerIter : public std::vector<int>::iterator
{...};

My concern is whether the standard might allow an implementation to use
a bare pointer as an iterator.

Thanks,
Mark
Feb 17 '06 #1
12 2130
On 2006-02-17 13:53:22 -0500, Mark P
<us****@fall2005REMOVE.fastmailCAPS.fm> said:
Is it guaranteed that STL iterators are aggregate types and can thus be
derived from?
No.
I'm thinking along the lines of, for example:

class DerIter : public std::vector<int>::iterator
{...};

My concern is whether the standard might allow an implementation to use
a bare pointer as an iterator.


Yes, it does allow it (and many implementations do use raw pointers).
For example, there is nothing preventing std::vector<T>::iterator from
being of type (T*).

Additionally, even if the iterator is aggregate, it likely won't have a
virtual destructor.

What are you trying to accomplish that cannot be accomplished by using
a has-a relationship and providing a conversion operator?


--
Clark S. Cox, III
cl*******@gmail.com

Feb 17 '06 #2
On Fri, 17 Feb 2006 18:53:22 GMT, Mark P
<us****@fall2005REMOVE.fastmailCAPS.fm> wrote:
Is it guaranteed that STL iterators are aggregate types and can thus be
derived from?
No, an iterator may be a good old pointer.
I'm thinking along the lines of, for example:

class DerIter : public std::vector<int>::iterator
{...};

My concern is whether the standard might allow an implementation to use
a bare pointer as an iterator.


Why would you want to do that? You iterator wouldn't be polymorohic
anyway.

Best wishes,
Roland Pibinger
Feb 17 '06 #3
Clark S. Cox III wrote:

Additionally, even if the iterator is aggregate, it likely won't have a
virtual destructor.


Since the usual idiom is to pass iterators by value, this makes no
difference. I can't think of a situation where you'd want to delete an
iterator.

--

Pete Becker
Roundhouse Consulting, Ltd.
Feb 17 '06 #4
Roland Pibinger wrote:

Why would you want to do that? You iterator wouldn't be polymorohic
anyway.


Sure it would. At compile time, that is. Which is how iterators are
used: you instantiate algorithms on iterator types. Which, of course,
doesn't answer why someone would want to do it. But there's no inherent
reason it wouldn't work.

--

Pete Becker
Roundhouse Consulting, Ltd.
Feb 17 '06 #5
Clark S. Cox III wrote:
On 2006-02-17 13:53:22 -0500, Mark P
<us****@fall2005REMOVE.fastmailCAPS.fm> said:
Is it guaranteed that STL iterators are aggregate types and can thus
be derived from?

No.
I'm thinking along the lines of, for example:

class DerIter : public std::vector<int>::iterator
{...};

My concern is whether the standard might allow an implementation to
use a bare pointer as an iterator.

Yes, it does allow it (and many implementations do use raw pointers).
For example, there is nothing preventing std::vector<T>::iterator from
being of type (T*).

Additionally, even if the iterator is aggregate, it likely won't have a
virtual destructor.

What are you trying to accomplish that cannot be accomplished by using a
has-a relationship and providing a conversion operator?


Thanks for the info. I briefly considered the idea when dealing with a
design issue but quickly abandoned it. (It was something convoluted
involving a derived class virtual function using a covariant return type
to return something derived from an iterator, but it really wasn't
sensible.) At the time I posted, I was just curious to know the offical
rules.
Feb 17 '06 #6
"Mark P" <us****@fall2005REMOVE.fastmailCAPS.fm> wrote in message
news:CY*******************@newssvr12.news.prodigy. com...
Is it guaranteed that STL iterators are aggregate types and can thus be
derived from?


No.
Feb 17 '06 #7
"Andrew Koenig" <ar*@acm.org> wrote in news:EVsJf.36009$id5.11617@bgtnsc04-
news.ops.worldnet.att.net:
"Mark P" <us****@fall2005REMOVE.fastmailCAPS.fm> wrote in message
news:CY*******************@newssvr12.news.prodigy. com...
Is it guaranteed that STL iterators are aggregate types and can thus be
derived from?


No.


In fact, Scott Meyers in "Efective STL" item 27 says "... it is common for
implementations of string and vector to use pointers as iterators".
--
Life is complex, with real and imaginary parts.
Feb 18 '06 #8
On Fri, 17 Feb 2006 15:26:53 -0500, Pete Becker <pe********@acm.org>
wrote:
Roland Pibinger wrote:
Why would you want to do that? You iterator wouldn't be polymorohic
anyway.
Sure it would. At compile time, that is.


I really suggest to limit the meaning of polymorphism to 'late
binding'. Otherswise someone could also rightfully speak of e.g.
'preprocessor polymorphism'.
Which is how iterators are
used: you instantiate algorithms on iterator types. Which, of course,
doesn't answer why someone would want to do it. But there's no inherent
reason it wouldn't work.

--

Pete Becker
Roundhouse Consulting, Ltd.


Oh, new signature
Feb 18 '06 #9
Roland Pibinger wrote:
On Fri, 17 Feb 2006 15:26:53 -0500, Pete Becker <pe********@acm.org>
wrote:
Roland Pibinger wrote:
Why would you want to do that? You iterator wouldn't be polymorohic
anyway.


Sure it would. At compile time, that is.

I really suggest to limit the meaning of polymorphism to 'late
binding'. Otherswise someone could also rightfully speak of e.g.
'preprocessor polymorphism'.


I strongly suggest qualifying the word polymorphism if
you want to speak of only one kind, such as runtime
polymorphism. The term "polymorphism" is a general one,
covering overloading, template mechanisms, virtual
dispatch and more. Sometimes in context it's clear
which we mean; sometimes it's not.

There is a move to simplify (I'll try to avoid saying
"dumb down") terminology so that we can act as if
programming is easier than actually it is. Simplistic
terminology actually makes mastering the subject harder,
not easier, by generating misunderstandings.

-- James
Feb 18 '06 #10
Roland Pibinger wrote:
On Fri, 17 Feb 2006 15:26:53 -0500, Pete Becker <pe********@acm.org>
wrote:
Roland Pibinger wrote:
Why would you want to do that? You iterator wouldn't be polymorohic
anyway.


Sure it would. At compile time, that is.

I really suggest to limit the meaning of polymorphism to 'late
binding'. Otherswise someone could also rightfully speak of e.g.
'preprocessor polymorphism'.


Well, the term "compile-time polymorphism" is fairly common these days.

--

Pete Becker
Roundhouse Consulting, Ltd.
Feb 18 '06 #11
"Pete Becker" <pe********@acm.org> wrote in message
news:ko********************@giganews.com...
Roland Pibinger wrote:
On Fri, 17 Feb 2006 15:26:53 -0500, Pete Becker <pe********@acm.org>
wrote:
Roland Pibinger wrote:

Why would you want to do that? You iterator wouldn't be polymorohic
anyway.

Sure it would. At compile time, that is.

I really suggest to limit the meaning of polymorphism to 'late
binding'. Otherswise someone could also rightfully speak of e.g.
'preprocessor polymorphism'.


Well, the term "compile-time polymorphism" is fairly common these days.


Perhaps this one is too general - though usually this term refers to
templates, overloading resolution is also compile time.

-- EK
Feb 18 '06 #12
Csaba wrote:
"Andrew Koenig" <ar*@acm.org> wrote in
news:EVsJf.36009$id5.11617@bgtnsc04- news.ops.worldnet.att.net:
"Mark P" <us****@fall2005REMOVE.fastmailCAPS.fm> wrote in message
news:CY*******************@newssvr12.news.prodigy. com...
Is it guaranteed that STL iterators are aggregate types and can thus be
derived from?


No.


In fact, Scott Meyers in "Efective STL" item 27 says "... it is common for
implementations of string and vector to use pointers as iterators".

I guess this means that i can't overload a function like this:
void f(std::vector<C>::iterator>);
void f(C*);
Is this a general property of the types used in the standard library,
that they may randomly equal to some builtin type, and thereby
inhibit overloading?
--
stein

Feb 19 '06 #13

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

Similar topics

38
by: Grant Edwards | last post by:
In an interview at http://acmqueue.com/modules.php?name=Content&pa=showpage&pid=273 Alan Kay said something I really liked, and I think it applies equally well to Python as well as the languages...
26
by: Michael Klatt | last post by:
I am trying to write an iterator for a std::set that allows the iterator target to be modified. Here is some relvant code: template <class Set> // Set is an instance of std::set<> class...
28
by: Steven T. Hatton | last post by:
This may be another question having an obvious answer, but I'm not seeing it. I'm trying to create a class that derives from std::valarray<std::string>. I don't need a template, and I haven't come...
0
by: mailforpr | last post by:
Hi. Let me introduce an iterator to you, the so-called "Abstract Iterator" I developed the other day. I actually have no idea if there's another "Abstract Iterator" out there, as I have never...
15
by: Nindi73 | last post by:
HI If I define the class DoubleMap such that struct DoubleMap : public std::map<std::string, double>{}; Is there any overhead in calling std::map member functions ? Moreover are STL...
12
by: Shraddha | last post by:
Can I stop people by deriving my class? I mean I don't want my class to be as a base class... Can I do that?
17
by: mosfet | last post by:
Could someone tell me why it's considered as bad practice to inherit from STL container ? And when you want to customize a STL container, do you mean I need to write tons of code just to avoid to...
1
by: Christopher Pisz | last post by:
I set out to make a custom logger. Examining some other code laying around, I came across one that derived from ostream, and had a associated class derived from streambuf. Is this practice a good...
7
by: Christopher Pisz | last post by:
I found an article http://spec.winprog.org/streams/ as a starting point, but it seems to do alot of things that aren't very standard at all. One particular problem is, that he is using a vector as...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
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:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
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,...

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.