Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C++ > How to convert from std::list<T*>::iterator to std::list<const T*>::iterator?

Reply
Thread Tools

How to convert from std::list<T*>::iterator to std::list<const T*>::iterator?

 
 
PengYu.UT@gmail.com
Guest
Posts: n/a
 
      10-27-2005
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

 
Reply With Quote
 
 
 
 
Victor Bazarov
Guest
Posts: n/a
 
      10-27-2005
wrote:
> 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;

> But std::list<const T*>::const_iterator doens't give me this
> capability. So I want std::list<T*>::iterator.


Huh?

> 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
 
Reply With Quote
 
 
 
 
Kaz Kylheku
Guest
Posts: n/a
 
      10-27-2005

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?


How about

reinterpret_cast<std::list<const T *> &>(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<T *>::iterator>
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<int>::iterator>

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.

 
Reply With Quote
 
Neil Cerutti
Guest
Posts: n/a
 
      10-27-2005
On 2005-10-27, <> 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<int> > cpl;
return 0;
}

--
Neil Cerutti
 
Reply With Quote
 
PengYu.UT@gmail.com
Guest
Posts: n/a
 
      10-27-2005

Neil Cerutti wrote:
> On 2005-10-27, <> 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<int> > cpl;
> return 0;
> }


Do you know which boost package should I use?

Thanks,
Peng

 
Reply With Quote
 
Neil Cerutti
Guest
Posts: n/a
 
      10-28-2005
On 2005-10-27, <> wrote:
>
> Neil Cerutti wrote:
>> On 2005-10-27, <> 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<int> > 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
 
Reply With Quote
 
Jim Langston
Guest
Posts: n/a
 
      10-30-2005
<> wrote in message
news: oups.com...
> 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?


 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Re: How include a large array? Edward A. Falk C Programming 1 04-04-2013 08:07 PM
IsNumeric: Convert.ToInt32 vs. Convert.ToInt64 sck10 ASP .Net 4 09-03-2006 09:40 PM
To convert to J2SE 6 or not to convert, that is the question... Jaap Java 4 07-10-2006 09:03 AM
convert list of strings to set of regexes; convert list of strings to trie Klaus Neuner Python 7 07-26-2004 07:25 AM
Do I need to Convert with Convert.ToInt32(session("myNumber")) ? Andreas Klemt ASP .Net 1 07-23-2003 02:59 PM



Advertisments
 



1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57