Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C++ > Re: cast from list<T> to list<const T>

Reply
Thread Tools

Re: cast from list<T> to list<const T>

 
 
Nick Hounsome
Guest
Posts: n/a
 
      04-01-2004

"Jonathan Turkanis" <> wrote in message
news:c4fl16$2dma64$...
> "Alexander Malkis" <> wrote in
> message news:c4fk8v$1a3kv$...
> > //Consider:
> > class A { /*...*/ };
> >
> > template<class T> class list {/*... */ };
> >
> > void f(const list<const A*> lst) { /*...doesn't change the

> arg...*/ }
> >

>
> list<const A*> and list<A*> are unrelated types. Neither is
> convertible to the other. If you have a list<A*> mylist and you want a
> list<const A*>, you can do this:
>
> list<const A*> copy(mylist.begin(), mylist.end());
>
> Jonathan


You missed the fact that f doesn't even take a reference!
so

f( list<const A*>(lst.begin(),lst.end()) ) ;

is as efficient as it could possibly be anyway.

On a more helpful note I would suggest that f should be templateized
as

template <class Collection> void f(const Collection& c) .....

Then create a template wrapper to make any collection of X* appear as a
collection of const X*
and pass this to f.

If the functions only treat the list as a sequence then use a similar
approach but with
begin and end iterators instead - that way there are less functions to
implement in the wrapper.



 
Reply With Quote
 
 
 
 
Siemel Naran
Guest
Posts: n/a
 
      04-01-2004
"Nick Hounsome" <> wrote in message newshXac.7585

> On a more helpful note I would suggest that f should be templateized
> as
>
> template <class Collection> void f(const Collection& c) .....
>
> Then create a template wrapper to make any collection of X* appear as a
> collection of const X*
> and pass this to f.


This should work, but seems it may take more effort than a smart pointer as
you have to create iterator classes, fix all functions like push_back(),
back(), etc. And it doesn't generalize to all containers either; list and
deque have push_front, vector has reserve, list has sort etc. Furthermore,
it leads to an extra instantiation of Collection<list<T*>>.


 
Reply With Quote
 
 
 
 
Jonathan Turkanis
Guest
Posts: n/a
 
      04-01-2004

"Nick Hounsome" <> wrote in message
newshXac.7585$...
>
> "Jonathan Turkanis" <> wrote in message
> news:c4fl16$2dma64$...
> > "Alexander Malkis" <> wrote

in
> > message news:c4fk8v$1a3kv$...
> > > //Consider:
> > > class A { /*...*/ };
> > >
> > > template<class T> class list {/*... */ };
> > >
> > > void f(const list<const A*> lst) { /*...doesn't change the

> > arg...*/ }
> > >

> >
> > list<const A*> and list<A*> are unrelated types. Neither is
> > convertible to the other. If you have a list<A*> mylist and you

want a
> > list<const A*>, you can do this:
> >
> > list<const A*> copy(mylist.begin(), mylist.end());
> >
> > Jonathan

>
> You missed the fact that f doesn't even take a reference!


Huh? I did miss the fact that the OP wasn't talking about std::list,
but don't see how the fact that f doesn't take a reference is
relevant. I was just addressing the issue of convertibility. In
particular, standard container templates don't have converting
constructors which accept different specializations of the same
template. They achieve the same effect with greater generality by
allowing construction from iterator ranges.

That was my point. Now I see the template was a user defined template;
in that case, you have to use whatever conversions are provided by
that template. In particular, list<const A*> might have a constructor
accepting a list<A*>, and no constructors taking iterator ranges.

Jonathan

> so
>
> f( list<const A*>(lst.begin(),lst.end()) ) ;
>
> is as efficient as it could possibly be anyway.
>
> On a more helpful note I would suggest that f should be templateized
> as
>
> template <class Collection> void f(const Collection& c) .....
>
> Then create a template wrapper to make any collection of X* appear

as a
> collection of const X*
> and pass this to f.
>
> If the functions only treat the list as a sequence then use a

similar
> approach but with
> begin and end iterators instead - that way there are less functions

to
> implement in the wrapper.
>
>
>



 
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
Is the result of valid dynamic cast always equal to the result ofcorrespondent static cast? Pavel C++ 7 09-18-2010 11:35 PM
error C2440: 'return' : cannot convert from 'const char *' to 'const unsigned short *' Types pointed to are unrelated; conversion requires reinterpret_cast, C-style cast or function-style cast Abhijit Bhadra C++ 2 12-01-2004 04:43 PM
malloc - to cast or not to cast, that is the question... EvilRix C Programming 8 02-14-2004 12:08 PM
to cast or not to cast malloc ? MSG C Programming 38 02-10-2004 03:13 PM
how to cast system.intptr to struct BestNews ASP .Net 1 09-03-2003 09:04 AM



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