 |
C++Talk.NET C++ language newsgroups
|
| View previous topic :: View next topic |
| Author |
Message |
PengYu.UT@gmail.com Guest
|
Posted: Thu Oct 27, 2005 7:33 pm Post subject: How to convert from std::list<T*>::iterator to std::list<con |
|
|
Hi,
Suppose I have a list which contains pointers. I want the pointer got
by dereferencing the iterator be a pointer pointing to a const object.
But std::list<const T*>::const_iterator doens't give me this
capability. So I want std::list<T*>::iterator.
However, the container is of type std::list<T*>. How to get
std::list<const T*>::iterator?
Best wishes,
Peng
|
|
| Back to top |
|
 |
Victor Bazarov Guest
|
Posted: Thu Oct 27, 2005 7:47 pm Post subject: Re: How to convert from std::list<T*>::iterator to std::list |
|
|
[email]PengYu.UT (AT) gmail (DOT) com[/email] wrote:
| Quote: | Suppose I have a list which contains pointers. I want the pointer got
by dereferencing the iterator be a pointer pointing to a const object.
|
Store const pointers. Or convert upon extraction.
Object const* p = *iter;
| Quote: | But std::list<const T*>::const_iterator doens't give me this
capability. So I want std::list<T*>::iterator.
|
Huh?
| Quote: | However, the container is of type std::list<T*>. How to get
std::list<const T*>::iterator?
|
There is no way. You can copy std::list<T*> into std::list<T const*>,
but there is no conversion between the containers or between their
respective iterators. T* and T const* are different types and the
templates instantiated for them are not related.
V
|
|
| Back to top |
|
 |
Kaz Kylheku Guest
|
Posted: Thu Oct 27, 2005 7:59 pm Post subject: Re: How to convert from std::list<T*>::iterator to std::list |
|
|
[email]PengYu.UT (AT) gmail (DOT) com[/email] wrote:
| Quote: | Hi,
Suppose I have a list which contains pointers. I want the pointer got
by dereferencing the iterator be a pointer pointing to a const object.
But std::list<const T*>::const_iterator doens't give me this
capability. So I want std::list<T*>::iterator.
However, the container is of type std::list<T*>. How to get
std::list<const T*>::iterator?
|
How about
reinterpret_cast<std::list(original_list)
to interpret the list as a list of const T * pointers? It's a hack, but
innocous enough. The templates should be binary compatible, so you are
down to some obscure undefined behavior related to aliasing.
Or else you could implement an iterator proxy object which wraps around
an existing iterator but adds const qualification to any extracted
pointers.
converting_iter<const T *, std::list
cui = original_list.begin();
The converting_iter template has two parameters: the iterator type to
be wrapped, and the type that the extracted values should be cast to.
Of course converting_iter has a converting constructor that takes a
reference to the iterator type being wrapped, so it can swallow the
object being proxied. Supporting assigment from that type might be
handy too.
It has to proxy all of the iter operations and pass them to the
captured object: things like ++, and so on. The ones that pull out a T
* are intercepted and wrapped with a static_cast<const T *>.
This could be used in other ways, e.g.
converting_iter<float, std::vector
would give you an converting iterator type that wraps iterators for a
std::vector<int>, with an interator that pulls out the values converted
to float.
|
|
| Back to top |
|
 |
Neil Cerutti Guest
|
Posted: Thu Oct 27, 2005 8:18 pm Post subject: Re: How to convert from std::list<T*>::iterator to std::list |
|
|
On 2005-10-27, [email]PengYu.UT (AT) gmail (DOT) com[/email] <PengYu.UT (AT) gmail (DOT) com> wrote:
| Quote: | Hi,
Suppose I have a list which contains pointers. I want the
pointer got by dereferencing the iterator be a pointer pointing
to a const object. But std::list<const T*>::const_iterator
doens't give me this capability. So I want
std::list<T*>::iterator.
However, the container is of type std::list<T*>. How to get
std::list<const T*>::iterator?
|
Try wrapping your pointers in a very simple wrapper class.
Boost:: might already provide such a thing. In which case, you
should use it instead of the pretty much untested example code
below.
/* Allow only const access to the item pointed to. */
template <typename T>
class const_ptr {
T* ptr;
public:
const_ptr(T* p = 0): ptr(p) { }
const T& operator*() { return *ptr; }
const T* operator->() { return ptr; }
};
int main()
{
/* For example: */
std::list< const_ptr cpl;
return 0;
}
--
Neil Cerutti
|
|
| Back to top |
|
 |
PengYu.UT@gmail.com Guest
|
Posted: Thu Oct 27, 2005 11:07 pm Post subject: Re: How to convert from std::list<T*>::iterator to std::list |
|
|
Neil Cerutti wrote:
| Quote: | On 2005-10-27, [email]PengYu.UT (AT) gmail (DOT) com[/email] <PengYu.UT (AT) gmail (DOT) com> wrote:
Hi,
Suppose I have a list which contains pointers. I want the
pointer got by dereferencing the iterator be a pointer pointing
to a const object. But std::list<const T*>::const_iterator
doens't give me this capability. So I want
std::list<T*>::iterator.
However, the container is of type std::list<T*>. How to get
std::list<const T*>::iterator?
Try wrapping your pointers in a very simple wrapper class.
Boost:: might already provide such a thing. In which case, you
should use it instead of the pretty much untested example code
below.
/* Allow only const access to the item pointed to. */
template <typename T
class const_ptr {
T* ptr;
public:
const_ptr(T* p = 0): ptr(p) { }
const T& operator*() { return *ptr; }
const T* operator->() { return ptr; }
};
int main()
{
/* For example: */
std::list< const_ptr cpl;
return 0;
}
|
Do you know which boost package should I use?
Thanks,
Peng
|
|
| Back to top |
|
 |
Neil Cerutti Guest
|
Posted: Fri Oct 28, 2005 12:45 pm Post subject: Re: How to convert from std::list<T*>::iterator to std::list |
|
|
On 2005-10-27, [email]PengYu.UT (AT) gmail (DOT) com[/email] <PengYu.UT (AT) gmail (DOT) com> wrote:
| Quote: |
Neil Cerutti wrote:
On 2005-10-27, [email]PengYu.UT (AT) gmail (DOT) com[/email] <PengYu.UT (AT) gmail (DOT) com> wrote:
Hi,
Suppose I have a list which contains pointers. I want the
pointer got by dereferencing the iterator be a pointer pointing
to a const object. But std::list<const T*>::const_iterator
doens't give me this capability. So I want
std::list<T*>::iterator.
However, the container is of type std::list<T*>. How to get
std::list<const T*>::iterator?
Try wrapping your pointers in a very simple wrapper class.
Boost:: might already provide such a thing. In which case, you
should use it instead of the pretty much untested example code
below.
/* Allow only const access to the item pointed to. */
template <typename T
class const_ptr {
T* ptr;
public:
const_ptr(T* p = 0): ptr(p) { }
const T& operator*() { return *ptr; }
const T* operator->() { return ptr; }
};
int main()
{
/* For example: */
std::list< const_ptr cpl;
return 0;
}
Do you know which boost package should I use?
|
Well, it turns out my suggestion was silly, and amounted to the
same thing as using a list<const T*>.
From Boost, what you might want is something from the group
of iterator adaptors. Specifically, the transform iterator
adaptor.
--
Neil Cerutti
|
|
| Back to top |
|
 |
Jim Langston Guest
|
Posted: Sun Oct 30, 2005 3:31 am Post subject: Re: How to convert from std::list<T*>::iterator to std::list |
|
|
<PengYu.UT (AT) gmail (DOT) com> wrote
| Quote: | Hi,
Suppose I have a list which contains pointers. I want the pointer got
by dereferencing the iterator be a pointer pointing to a const object.
But std::list<const T*>::const_iterator doens't give me this
capability. So I want std::list<T*>::iterator.
However, the container is of type std::list<T*>. How to get
std::list<const T*>::iterator?
Best wishes,
Peng
|
would std::list<T*>::const_iterator work for you?
|
|
| 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
|
|