C++Talk.NET Forum Index C++Talk.NET
C++ language newsgroups
 
Archives   FAQFAQ   SearchSearch   MemberlistMemberlist   UsergroupsUsergroups   RegisterRegister 
 ProfileProfile   Log in to check your private messagesLog in to check your private messages   Log inLog in 

iterators::reference - what requirements on it are exposed?

 
Post new topic   Reply to topic    C++Talk.NET Forum Index -> C++ language (comp.lang.c++)
View previous topic :: View next topic  
Author Message
Alexander Stippler
Guest





PostPosted: Sun Dec 28, 2003 2:56 pm    Post subject: iterators::reference - what requirements on it are exposed? Reply with quote



Hi,

I've got a question concerning iterators. I'm writing some container class
and iterators upon it. I have to have
typedef typex pointer;
typedef typey reference;
to be standard conforming. But what semantic requirements does the standard
expose on type typey (and typex). If I define
reference operator*(),
does the object I return have to be (non-const) referencable or can I return
a temporary object of typey? If I'm not allowed to, how can I write some
iterator and especially operator*(), if I have to create its result on the
fly? I hope, I made my question understandable.

regards,
alex


Back to top
Nick Hounsome
Guest





PostPosted: Sun Dec 28, 2003 3:54 pm    Post subject: Re: iterators::reference - what requirements on it are expos Reply with quote




"Alexander Stippler" <stip (AT) mathematik (DOT) uni-ulm.de> wrote

Quote:
Hi,

I've got a question concerning iterators. I'm writing some container class
and iterators upon it. I have to have
typedef typex pointer;
typedef typey reference;
to be standard conforming. But what semantic requirements does the
standard
expose on type typey (and typex). If I define
reference operator*(),

I forget all the details as to why but apparently to be a standard container
it turns out that pointer and reference have to be the obvious types (T*,
T&) (perhaps const or volatile but not proxy classes).

However you don't have to create standard containers and even the standard
contains "non-standard" containers in particular vector<bool> precisely
because reference isn't bool& and therefore you cannot take the address of
one to get bool*.

I think I MAY have come across all this in "Effective C++" or "More
Effective C++"

The main reason to give the typedef is so that standard algorithms can work
with your iterators.
If an algorith is supplied an iterator it cannot know directly what the
fundamental type is so it generally needs to use the typedefs

Quote:
does the object I return have to be (non-const) referencable or can I
return
a temporary object of typey? If I'm not allowed to, how can I write some

vector<bool> uses something that is not bool& and I don't know of any
practical drawback.

Quote:
iterator and especially operator*(), if I have to create its result on the
fly? I hope, I made my question understandable.

regards,
alex





Back to top
Alexander Stippler
Guest





PostPosted: Mon Dec 29, 2003 5:10 pm    Post subject: Re: iterators::reference - what requirements on it are expos Reply with quote



Nick Hounsome wrote:

Quote:

"Alexander Stippler" <stip (AT) mathematik (DOT) uni-ulm.de> wrote in message
news:3feeedf6 (AT) news (DOT) uni-ulm.de...
Hi,

I've got a question concerning iterators. I'm writing some container
class and iterators upon it. I have to have
typedef typex pointer;
typedef typey reference;
to be standard conforming. But what semantic requirements does the
standard
expose on type typey (and typex). If I define
reference operator*(),

I forget all the details as to why but apparently to be a standard
container it turns out that pointer and reference have to be the obvious
types (T*, T&) (perhaps const or volatile but not proxy classes).

However you don't have to create standard containers and even the standard
contains "non-standard" containers in particular vector<bool> precisely
because reference isn't bool& and therefore you cannot take the address of
one to get bool*.

I think I MAY have come across all this in "Effective C++" or "More
Effective C++"

The main reason to give the typedef is so that standard algorithms can
work with your iterators.
If an algorith is supplied an iterator it cannot know directly what the
fundamental type is so it generally needs to use the typedefs

does the object I return have to be (non-const) referencable or can I
return
a temporary object of typey? If I'm not allowed to, how can I write some

vector<bool> uses something that is not bool& and I don't know of any
practical drawback.

iterator and especially operator*(), if I have to create its result on
the fly? I hope, I made my question understandable.

regards,
alex



Indeed both the icc-STL and gcc-STL do not return references for operator*()
in some more general places, as I shortly noticed. Look at this, taken from
reverse_iterator implementation:

reference operator*() const
{ // return designated value
_RanIt _Tmp = current;
return (*--_Tmp);
}

Thanks for your answer. I want to use STL algorithms and that is exactly the
extent to which I want to be standard conforming.

regards,
alex

Back to top
Display posts from previous:   
Post new topic   Reply to topic    C++Talk.NET Forum Index -> C++ language (comp.lang.c++) All times are GMT
Page 1 of 1

 
Jump to:  
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum


Powered by phpBB © 2001, 2006 phpBB Group
SEO toolkit © 2004-2006 webmedic.