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

A

Alexander Stippler

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
 
N

Nick Hounsome

Alexander Stippler said:
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
 
A

Alexander Stippler

Nick said:
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

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
 

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

No members online now.

Forum statistics

Threads
473,755
Messages
2,569,536
Members
45,011
Latest member
AjaUqq1950

Latest Threads

Top