Syntactical issue: should STL list Iterator be overloaded to a pointer to the current element?

O

Olumide

Hello,

To begin with, I'm no STL expert. I've only just begun to use and
appreciate the power of STL.

Okay, so I have a list:

list<int> myLuckyNumbers;
list<int>::iterator intList_Iterator = myLuckyNumbers->begin();

while( intList_Iterator != myLuckyNumbers->end() )
{
int *integerPointer = &(*intList_Iterator);

intList_Iterator++;
}


In order to get a pointer to each element of the list, I need to use
the unintuitive expression, &(*intList_Iterator). My worry is that
because the dereference (*) and "address of" (&) operators, cancel
each other, shouldn't intList_Iterator be interpreted as a pointer to
a int, if assigned to an int?

Also, is there a better way getting the pointer to the current in in
the list?

Thanks,

- Olumide
 
V

Victor Bazarov

Olumide said:
To begin with, I'm no STL expert. I've only just begun to use and
appreciate the power of STL.

Okay, so I have a list:

list<int> myLuckyNumbers;
list<int>::iterator intList_Iterator = myLuckyNumbers->begin();

while( intList_Iterator != myLuckyNumbers->end() )
{
int *integerPointer = &(*intList_Iterator);

intList_Iterator++;
}


In order to get a pointer to each element of the list, I need to use
the unintuitive expression, &(*intList_Iterator). My worry is that
because the dereference (*) and "address of" (&) operators, cancel
each other,

It's a misconception. The dereference operator does not cancel the
address-of operator. They can (and the dereference is) overloaded
for the iterator object. No canceling occurs.
shouldn't intList_Iterator be interpreted as a pointer to
a int, if assigned to an int?

Also, is there a better way getting the pointer to the current in in
the list?

Better? In what way "better"?

V
 
J

Jim Langston

Olumide said:
Better as in other than &(*intList_Iterator) .

&(*iterator)
is the common method used to get the address of what iterator points to.
After you see it a few times it no longer looks weird.
 
P

Pete Becker

&(*iterator)
is the common method used to get the address of what iterator points to.
After you see it a few times it no longer looks weird.

It looks weird that way. The parentheses aren't needed. <g>
 
B

Bo Persson

Olumide wrote:
::: On 2007-09-11 16:36:01 -0400, "Jim Langston"
::: <[email protected]> said:
:::
:::: ::::: On 11 Sep, 21:11, "Victor Bazarov" <[email protected]>
::::: wrote:
:::::: Better? In what way "better"?
:::
::::: Better as in other than &(*intList_Iterator) .
:::
:::: &(*iterator)
:::: is the common method used to get the address of what iterator
:::: points to. After you see it a few times it no longer looks weird.
:::
::: It looks weird that way. The parentheses aren't needed. <g>
::
:: &*iterator looks mucho weird :) .

Not really, if you think about it:

*iterator

gives you access to an object, so

&*iterator

is the address of that object.


You will hardly ever have to do this anyway, as in C++ code there is
not much use for a pointer, if you already have an interator.

Probably it is mostly used in interfacing to old C code APIs, which in
C++ looks mucho weirdo anyway.


Bo Persson
 
O

Olumide

Olumide wrote:
:: &*iterator looks mucho weird :) .

Not really, if you think about it:

*iterator

gives you access to an object, so

&*iterator

is the address of that object.

Thanks. I know unary operators are right associative ... Its just that
dereferencing and then taking the address of a variable *plain* C++ is
horribly redundant redundant.
 
T

Tristan Wibberley

Hello,

To begin with, I'm no STL expert. I've only just begun to use and
appreciate the power of STL.

Okay, so I have a list:

list<int> myLuckyNumbers;
list<int>::iterator intList_Iterator = myLuckyNumbers->begin();

while( intList_Iterator != myLuckyNumbers->end() )
{
int *integerPointer = &(*intList_Iterator);

intList_Iterator++;
}


In order to get a pointer to each element of the list, I need to use
the unintuitive expression, &(*intList_Iterator). My worry is that
because the dereference (*) and "address of" (&) operators, cancel
each other, shouldn't intList_Iterator be interpreted as a pointer to
a int, if assigned to an int?

Also, is there a better way getting the pointer to the current in in
the list?

You could try:

template<typename T>
T* iter_pointer(T* iter)
{
return iter;
}

template<typename T>
typename std::iterator_traits<T>::pointer iter_pointer(const T& iter)
{
return iter.operator->();
}

....

iterator_traits<list<int>::iterator>::pointer integerPointer
= iter_pointer(intList_Iterator);

This neatly avoids going through *intList_Iterator so iterators whose
pointer types are smart and reference count and stuff will work without
undefined behaviour until C++ finally supports generic smart references.

Unfortunately I don't know how to cope with the C++0x proposal to allow
nonmember operator-> :(

--
Tristan Wibberley

Any opinion expressed is mine (or else I'm playing devils advocate for
the sake of a good argument). My employer had nothing to do with this
communication.
 
R

Richard Herring

Hello,

To begin with, I'm no STL expert. I've only just begun to use and
appreciate the power of STL.

Okay, so I have a list:

list<int> myLuckyNumbers;
list<int>::iterator intList_Iterator = myLuckyNumbers->begin();

while( intList_Iterator != myLuckyNumbers->end() )
{
int *integerPointer = &(*intList_Iterator);

intList_Iterator++;
}


In order to get a pointer to each element of the list,

Nobody in this thread seems to have asked what to me is the obvious
question: why do you _want_ a pointer to each element? If all you need
is something that "points at" the element, the iterator is already a
model of the concept of pointer, so (as Bo Persson points out) for most
purposes it's sufficient to use the iterator directly.
 

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,744
Messages
2,569,482
Members
44,901
Latest member
Noble71S45

Latest Threads

Top