 |
C++Talk.NET C++ language newsgroups
|
| View previous topic :: View next topic |
| Author |
Message |
Yuriy Solodkyy Guest
|
Posted: Wed Sep 07, 2005 7:09 pm Post subject: Relationship between pointer and iterator associated types |
|
|
Hi guys,
I don't see any requirement for Container::pointer associated type in the
standard, though it is mentioned on SGI's STL website (however const_pointer
is not mentioned there either). My question is what is the relationship
between pointer and iterator associated types? What's the difference between
them? When to use one and when the other? Should there be a conversion from
iterator to pointer (there seems to be no such in gcc implementation)?
Thanks in advance,
Yuriy
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]
|
|
| Back to top |
|
 |
Victor Bazarov Guest
|
Posted: Thu Sep 08, 2005 9:16 am Post subject: Re: Relationship between pointer and iterator associated typ |
|
|
Yuriy Solodkyy wrote:
| Quote: | I don't see any requirement for Container::pointer associated type in the
standard, though it is mentioned on SGI's STL website (however const_pointer
is not mentioned there either). My question is what is the relationship
between pointer and iterator associated types?
|
None, AFAICS. 'pointer' is a typedef from 'Allocator::pointer', where
'Allocator' is the template argument.
| Quote: | What's the difference between
them?
|
They are different. IOW, they are not the same.
| Quote: | When to use one and when the other?
|
Many standard algorithms are designed to use iterators. Use iterators
with those. Use pointers when pointers are expected.
| Quote: | Should there be a conversion from
iterator to pointer (there seems to be no such in gcc implementation)?
|
Dereferencing 'Container::iterator' gives you 'Container::reference',
probably, and if you use '&' on that, you're going to probably get the
same type as 'Container::pointer'.
V
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]
|
|
| Back to top |
|
 |
Carl Barron Guest
|
Posted: Thu Sep 08, 2005 9:23 am Post subject: Re: Relationship between pointer and iterator associated typ |
|
|
In article <dfncfa$b48$1 (AT) news (DOT) tamu.edu>, Yuriy Solodkyy
<solodon (AT) tamudotedu (DOT) invalid> wrote:
| Quote: | Hi guys,
I don't see any requirement for Container::pointer associated type in the
standard, though it is mentioned on SGI's STL website (however const_pointer
is not mentioned there either). My question is what is the relationship
between pointer and iterator associated types? What's the difference between
them? When to use one and when the other? Should there be a conversion from
iterator to pointer (there seems to be no such in gcc implementation)?
There is no requirement for Container::pointer. All I could guess |
its something like typedef Container::value_type *pointer; and not
useful as if you really need a type pointer to container element, then
define one yourself where its needed.
for a forward,bidirectional or random access iterator with value_type V.
typedef reference &V ; // or const &V for a constant iterator;
typedef pointer *V; // or const *V for a constant iterator.
for input iterators they are iterator dependent but often as above.
for output iterators the types are not needed.
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]
|
|
| Back to top |
|
 |
kanze Guest
|
Posted: Thu Sep 08, 2005 1:45 pm Post subject: Re: Relationship between pointer and iterator associated typ |
|
|
Victor Bazarov wrote:
| Quote: | Yuriy Solodkyy wrote:
I don't see any requirement for Container::pointer
associated type in the standard, though it is mentioned on
SGI's STL website (however const_pointer is not mentioned
there either). My question is what is the relationship
between pointer and iterator associated types?
None, AFAICS. 'pointer' is a typedef from 'Allocator::pointer', where
'Allocator' is the template argument.
What's the difference between them?
They are different. IOW, they are not the same.
|
They do bear one simularity: both can be used to access a
specific element in the container, by means of the * operator.
| Quote: | When to use one and when the other?
Many standard algorithms are designed to use iterators. Use
iterators with those. Use pointers when pointers are
expected.
|
Attention: a pointer in C++ is more than just a pointer. It is
also an iterator into a C style array. Be very careful when
using pointers that the function being called doesn't expect an
iterator into a C style array.
| Quote: | Should there be a conversion from iterator to pointer (there
seems to be no such in gcc implementation)?
Dereferencing 'Container::iterator' gives you
'Container::reference', probably, and if you use '&' on that,
you're going to probably get the same type as
'Container::pointer'.
|
Probably. But is it guaranteed ?
--
James Kanze GABI Software
Conseils en informatique orientée objet/
Beratung in objektorientierter Datenverarbeitung
9 place Sémard, 78210 St.-Cyr-l'École, France, +33 (0)1 30 23 00 34
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]
|
|
| Back to top |
|
 |
Victor Bazarov Guest
|
Posted: Fri Sep 09, 2005 3:12 pm Post subject: Re: Relationship between pointer and iterator associated typ |
|
|
kanze wrote:
| Quote: | Victor Bazarov wrote:
Dereferencing 'Container::iterator' gives you
'Container::reference', probably, and if you use '&' on that,
you're going to probably get the same type as
'Container::pointer'.
Probably. But is it guaranteed ?
|
Nothing is guaranteed AFA real world is concerned.
V
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]
|
|
| Back to top |
|
 |
Yuriy Solodkyy Guest
|
Posted: Sat Sep 10, 2005 1:35 pm Post subject: Re: Relationship between pointer and iterator associated typ |
|
|
| Quote: | They do bear one simularity: both can be used to access a
specific element in the container, by means of the * operator.
|
If I understand it right, the main difference of pointer and iterator is
that iterator goes only one way: by dereferencing iterator you get a
reference to an element. Taking address of that reference won't return you
iterator back but a pointer. This is different from built-in pointers as by
taking address of dereferenced reference you'll get the same pointer back.
This also raises a question whether pointer type should implement other
things implemented by iterator e.g. operator++() or should it just behave as
a trivial iterator? Is this defined somewhere?
| Quote: | Should there be a conversion from iterator to pointer (there
seems to be no such in gcc implementation)?
Dereferencing 'Container::iterator' gives you
'Container::reference', probably, and if you use '&' on that,
you're going to probably get the same type as
'Container::pointer'.
Probably. But is it guaranteed ?
|
I talked to other people here and they said that pointer is also a type that
operator-> returns on iterator and is meant to provide for cases when T* is
not sufficient for implementation and some sort of proxy is needed.
Basically for vector<bool> you can't write bool* p = &v[4] and should write
vector<bool>::pointer p = &v[4]
The reason I'm asking all this is I have to implement an iterator where I
have to use proxies for both pointer and reference. Logically iterator in
this case is a subclass of pointer as it contains more information then the
pointer and behaves like one. Having that should it be convertible to
pointer? GCC doesn't provide such conversion but should it logically be
there?
Yuriy
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]
|
|
| Back to top |
|
 |
Rob Guest
|
Posted: Sun Sep 11, 2005 3:15 pm Post subject: Re: Relationship between pointer and iterator associated typ |
|
|
Yuriy Solodkyy wrote:
| Quote: | They do bear one simularity: both can be used to access a
specific element in the container, by means of the * operator.
If I understand it right, the main difference of pointer and iterator
is that iterator goes only one way: by dereferencing iterator you get
a reference to an element. Taking address of that reference won't
return you iterator back but a pointer. This is different from
built-in pointers as by taking address of dereferenced reference
you'll get the same pointer back.
|
Sure. The basic relationship is that a pointer is a type of iterator,
but not all iterators are pointers.
| Quote: |
This also raises a question whether pointer type should implement
other things implemented by iterator e.g. operator++() or should it
just behave as a trivial iterator? Is this defined somewhere?
|
Incrementing is allowed for pointers to anything except void.
| Quote: |
Should there be a conversion from iterator to pointer (there
seems to be no such in gcc implementation)?
Dereferencing 'Container::iterator' gives you
'Container::reference', probably, and if you use '&' on that,
you're going to probably get the same type as
'Container::pointer'.
Probably. But is it guaranteed ?
I talked to other people here and they said that pointer is also a
type that operator-> returns on iterator and is meant to provide for
cases when T* is not sufficient for implementation and some sort of
proxy is needed. Basically for vector<bool> you can't write bool* p
= &v[4] and should write vector<bool>::pointer p = &v[4]
|
I might be out of date, but I thought though that std::vector<bool> was
disallowed.
| Quote: |
The reason I'm asking all this is I have to implement an iterator
where I have to use proxies for both pointer and reference. Logically
iterator in this case is a subclass of pointer as it contains more
information then the pointer and behaves like one. Having that should
it be convertible to pointer? GCC doesn't provide such conversion but
should it logically be there?
|
The typical way to get a pointer from an iterator is to dereference the
iterator (which yields a reference to the object) and take it's address.
While I haven't seen the rationale for this explained specifically
anywhere, I would assume the reason conversion from iterator to pointer
is disallowed is, for that iterators from containers that don't store
objects contiguously, that arithetic on those iterators works
differently than it does for pointers.
For example;
#include <list>
#include <vector>
#include <assert.h> // C header. I'm lazy
int main()
{
std::vector<int> x(42);
std::vector<int>::iterator ix = x.begin();
int *px = &(*ix); // one way of getting pointer to first element
of x
++px;
++ix;
assert(&(*ix) == px); // this assertion is guaranteed to succeed
// the following is like the above, with list substituted for vector
std::list<int> x(42);
std::list<int>::iterator ix = x.begin();
int *px = &(*ix); // one way of getting pointer to first element
of x
++px;
++ix;
assert(&(*ix) == px); // this assertion is NOT guaranteed to
succeed
}
So, while a direct conversion from iterator to pointer would make some
sort of sense for a vector, it would not for a list.
So, while it's not necessarily wrong to support conversion from
iterator to pointer, you will need to be sure that it makes sense for
your container type
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]
|
|
| Back to top |
|
 |
Carl Barron Guest
|
Posted: Sun Sep 11, 2005 9:19 pm Post subject: Re: Relationship between pointer and iterator associated typ |
|
|
In article <dft7j3$9qt$1 (AT) news (DOT) tamu.edu>, Yuriy Solodkyy
<solodon (AT) tamudotedu (DOT) invalid> wrote:
| Quote: |
The reason I'm asking all this is I have to implement an iterator where I
have to use proxies for both pointer and reference. Logically iterator in
this case is a subclass of pointer as it contains more information then the
pointer and behaves like one. Having that should it be convertible to
pointer? GCC doesn't provide such conversion but should it logically be
there?
|
Be warned if your iterator provides an iterator category other than
std::input_iterator_tag, or std::output_iterator_tag, it is not valid
according to the standard since reference must be &value_type and
pointer must be *value_type, for forward ,bidirectional or random access
iterators.
If you really have a need for conversion to a raw pointer, provide a
member function to produce one. An iterator is not neccesarily
convertable to a pointer [example std::istream_iterator<int>].
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]
|
|
| Back to top |
|
 |
Yuriy Solodkyy Guest
|
Posted: Mon Sep 12, 2005 9:27 am Post subject: Re: Relationship between pointer and iterator associated typ |
|
|
| Quote: | Sure. The basic relationship is that a pointer is a type of iterator,
but not all iterators are pointers.
|
That's not what I understood: accordingly to what I wrote above it looked to
me as all iterators are pointers (not in terms of raw pointers but in terms
of iterator_traits<Iterator>::pointer type) and therefore maybe there should
be a conversion from iterator to its corresponding pointer type.
| Quote: | This also raises a question whether pointer type should implement
other things implemented by iterator e.g. operator++() or should it
just behave as a trivial iterator? Is this defined somewhere?
Incrementing is allowed for pointers to anything except void.
|
Does it mean that iterator_traits<Iterator>::pointer type should basically
duplicate all the operations that Iterator has or can they be just limited
to dereferencing? If it should duplicate everything then I see no point in
having reference::operator&() return pointer type but rather again iterator
type.
| Quote: | Should there be a conversion from iterator to pointer (there
seems to be no such in gcc implementation)?
Dereferencing 'Container::iterator' gives you
'Container::reference', probably, and if you use '&' on that,
you're going to probably get the same type as
'Container::pointer'.
Probably. But is it guaranteed ?
I talked to other people here and they said that pointer is also a
type that operator-> returns on iterator and is meant to provide for
cases when T* is not sufficient for implementation and some sort of
proxy is needed. Basically for vector<bool> you can't write bool* p
= &v[4] and should write vector<bool>::pointer p = &v[4]
I might be out of date, but I thought though that std::vector<bool> was
disallowed.
|
Still in the standard AFAIK
| Quote: | While I haven't seen the rationale for this explained specifically
anywhere, I would assume the reason conversion from iterator to pointer
is disallowed is, for that iterators from containers that don't store
objects contiguously, that arithetic on those iterators works
differently than it does for pointers.
|
I think you assume here by pointer a raw pointer type while I assume by
pointer
an iterator_traits<Iterator>::pointer type, which in my oppinion needn't be
just a raw pointer (taking into account some other posts to this thread, I'm
not sure what standard sais on this point).
Specifically you write in your code:
| Quote: | std::vector<int> x(42);
std::vector<int>::iterator ix = x.begin();
int *px = &(*ix); // one way of getting pointer to first element
|
which I think is not a fully generic code as it won't work for containers
that implement pointer type differently (e.g. vector<bool>). A better
version should do the following:
std::vector<int>::pointer px = &(*ix);
Your reasoning in code though is absolutely right if we postulate that
pointer type of an iterator is guaranted to be a value_type*.
Yuriy
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]
|
|
| Back to top |
|
 |
Yuriy Solodkyy Guest
|
Posted: Mon Sep 12, 2005 9:28 am Post subject: Re: Relationship between pointer and iterator associated typ |
|
|
| Quote: | Be warned if your iterator provides an iterator category other than
std::input_iterator_tag, or std::output_iterator_tag, it is not valid
according to the standard since reference must be &value_type and
pointer must be *value_type, for forward ,bidirectional or random access
iterators.
|
Does it mean that vector<bool>::iterator is not a random access iterator?
Where actually in the standard does it specify that pointer must be
value_type* and reference value_type& for forward iterators and higher?
| Quote: | If you really have a need for conversion to a raw pointer, provide a
member function to produce one. An iterator is not neccesarily
convertable to a pointer [example std::istream_iterator<int>].
|
By type pointer I didn't mean only raw pointers, though I understand that if
it is defined by the standard as you said, it will be the same. I need
pointer type to be a proxy as well, so it is not a raw pointer. I don't
specifically have a need to provide such conversion, I was just wondering
whether such should be provided for genericity.
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]
|
|
| Back to top |
|
 |
John Potter Guest
|
Posted: Mon Sep 12, 2005 9:34 am Post subject: Re: Relationship between pointer and iterator associated typ |
|
|
On 11 Sep 2005 17:19:01 -0400, Carl Barron <cbarron413 (AT) adelphia (DOT) net>
wrote:
| Quote: | In article <dft7j3$9qt$1 (AT) news (DOT) tamu.edu>, Yuriy Solodkyy
[email]solodon (AT) tamudotedu (DOT) inva[/email]lid> wrote:
The reason I'm asking all this is I have to implement an iterator where I
have to use proxies for both pointer and reference. Logically iterator in
this case is a subclass of pointer as it contains more information then the
pointer and behaves like one. Having that should it be convertible to
pointer? GCC doesn't provide such conversion but should it logically be
there?
Be warned if your iterator provides an iterator category other than
std::input_iterator_tag, or std::output_iterator_tag, it is not valid
according to the standard since reference must be &value_type
|
I assume that you mean value_type&. I find no such requirement. It is
true that *iter must return a T&; however, there is no requirement that
iter::reference be anything in particular and no requirement that
operator* return an iter::reference.
| Quote: | and
pointer must be *value_type, for forward ,bidirectional or random access
iterators.
|
I assume value_type*. I find no requirements on iter::pointer at all.
It must be defined, but has no meaning.
Proxie iterators are not allowed, so vector<bool> is invalid? I have
lost track of where the standard has gone on this.
John
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]
|
|
| Back to top |
|
 |
kanze Guest
|
Posted: Mon Sep 12, 2005 10:34 am Post subject: Re: Relationship between pointer and iterator associated typ |
|
|
Yuriy Solodkyy wrote:
| Quote: | They do bear one simularity: both can be used to access a
specific element in the container, by means of the *
operator.
If I understand it right, the main difference of pointer and
iterator is that iterator goes only one way: by dereferencing
iterator you get a reference to an element. Taking address of
that reference won't return you iterator back but a pointer.
This is different from built-in pointers as by taking address
of dereferenced reference you'll get the same pointer back.
|
The main difference is that you can get a pointer to any
object. An iterator of a given type can only refer to objects
in a specific type of container.
| Quote: | This also raises a question whether pointer type should
implement other things implemented by iterator
e.g. operator++() or should it just behave as a trivial
iterator? Is this defined somewhere?
|
Arithmetic is defined on pointers, as long as the pointer (or
pointers) involved designate different elements of the same C
style array.
| Quote: | Should there be a conversion from iterator to pointer
(there seems to be no such in gcc implementation)?
Dereferencing 'Container::iterator' gives you
'Container::reference', probably, and if you use '&' on
that, you're going to probably get the same type as
'Container::pointer'.
Probably. But is it guaranteed ?
I talked to other people here and they said that pointer is
also a type that operator-> returns on iterator and is meant
to provide for cases when T* is not sufficient for
implementation and some sort of proxy is needed. Basically
for vector<bool> you can't write bool* p = &v[4] and should
write vector<bool>::pointer p = &v[4]
The reason I'm asking all this is I have to implement an
iterator where I have to use proxies for both pointer and
reference.
|
There's no such thing as a proxy for a reference. You can't
overload operator.().
| Quote: | Logically iterator in this case is a subclass of pointer
|
No. Logically, iterators are iterators, and pointers are
pointers. For historical reasons, pointers can, in certain
specific cases, be used as iterators, but conceptually, the two
are different, unrelated things.
| Quote: | as it contains more information then the pointer and behaves
like one. Having that should it be convertible to pointer?
GCC doesn't provide such conversion but should it logically be
there?
|
No.
--
James Kanze GABI Software
Conseils en informatique orientée objet/
Beratung in objektorientierter Datenverarbeitung
9 place Sémard, 78210 St.-Cyr-l'École, France, +33 (0)1 30 23 00 34
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]
|
|
| Back to top |
|
 |
Carl Barron Guest
|
Posted: Mon Sep 12, 2005 2:49 pm Post subject: Re: Relationship between pointer and iterator associated typ |
|
|
In article <EebVe.11217$FW1.2255 (AT) newsread3 (DOT) news.atl.earthlink.net>,
John Potter <jpotter (AT) lhup (DOT) edu> wrote:
| Quote: | Proxie iterators are not allowed, so vector<bool> is invalid? I have
lost track of where the standard has gone on this.
I got the punctuation backwards:) |
the proxy iterator problem is what the std::vector<bool>
'controversy' is about.
A draft from 2003 actually shows T or const T & as result of
operator *(), but it must be a typo of some kind as then *x = t does
not modify the T 'pointed to by' x. The 1998 standard states T &,
T == value_type.
This gets confusing and is one reason the boost proposal for a
separation of iteration and dereferencing [which did not make tr1] was
suggested. There
can not be any proxied forward iterators, to my knowledge but
I could be wrong on this.
the standard states that it is only neccesary to define
value_type,difference_type and iterator_category for algorithms.
it actually does not specify what pointer and reference should be
defined as, except for the pointer specializations of
std::iterator_traits< T *>, std::iterator_traits<const T *> do define
pointer as a T *, and a reference as a T & and the value_type as T.
Pointer and reference were added for proxies or something, but the
standard [1998 standard] does not even require them to exist.
going by table 74 and
example 24.3.3 para 4 the meanings of are confusing and need fixing
this is a bidirectional iterator by the example but not by table 74/75.
Interesting.
the 2003 draft I have states that reference is the result of operator
*(); and pointer is the result of operator ->(). [1998 does not]
the 2005 draft apparently I mis renamed it on this machine is
N1804=05-0064 dated 2005--04-27.
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]
|
|
| Back to top |
|
 |
Gabriel Dos Reis Guest
|
Posted: Mon Sep 12, 2005 5:06 pm Post subject: Re: Relationship between pointer and iterator associated typ |
|
|
"Rob" <nospam (AT) nonexistant (DOT) com> writes:
[...]
| Quote: | I talked to other people here and they said that pointer is also a
type that operator-> returns on iterator and is meant to provide for
cases when T* is not sufficient for implementation and some sort of
proxy is needed. Basically for vector<bool> you can't write bool* p
= &v[4] and should write vector<bool>::pointer p = &v[4]
I might be out of date, but I thought though that std::vector<bool> was
disallowed.
|
Who did that? vector<bool> may be a problem is search of solution but I
haven't seen the C++ standard says it is disallowed.
--
Gabriel Dos Reis
[email]gdr (AT) integrable-solutions (DOT) net[/email]
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]
|
|
| Back to top |
|
 |
Gabriel Dos Reis Guest
|
Posted: Mon Sep 12, 2005 5:07 pm Post subject: Re: Relationship between pointer and iterator associated typ |
|
|
"kanze" <kanze (AT) gabi-soft (DOT) fr> writes:
[...]
| Quote: | I talked to other people here and they said that pointer is
also a type that operator-> returns on iterator and is meant
to provide for cases when T* is not sufficient for
implementation and some sort of proxy is needed. Basically
for vector<bool> you can't write bool* p = &v[4] and should
write vector<bool>::pointer p = &v[4]
The reason I'm asking all this is I have to implement an
iterator where I have to use proxies for both pointer and
reference.
There's no such thing as a proxy for a reference. You can't
overload operator.().
|
operator(). does not make sense for all references (e.g. bool&
int&, etc.). And not just because you can't overload operator(). means
you can't have a proxy for a reference.
As a matter of fact vector<bool>::reference is a class type.
(so is bitset<N>::reference).
--
Gabriel Dos Reis
[email]gdr (AT) integrable-solutions (DOT) net[/email]
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]
|
|
| Back to top |
|
 |
|
|
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
|
|