Safer casts

Yigal Chripun yigal100 at gmail.com
Sun May 11 17:16:55 PDT 2008


Koroskin Denis wrote:
> On Sun, 11 May 2008 19:47:14 +0400, Yigal Chripun <yigal100 at gmail.com>
> wrote:
> 
>> Janice Caron wrote:
>>> On 11/05/2008, Yigal Chripun <yigal100 at gmail.com> wrote:
>>>> I want to be able to do this:
>>>>  List!(T) col = new LinkedList!(T);
>>>>
>>>>  and be able to change the linked list to a vector in the future (or
>>>> any
>>>>  other suitable collection) with the changes limited to this line
>>>> alone.
>>>>  is it possible with templates?
>>>
> 
> Well, yes, is possible. Even with STL.
> 
> A typical trick I often do is this:
> 
> typedef std::vector<Item*> Items;
> Items items;
> 
> items.pushBack(new Item());
> Item* item = items.front();
> Items::iterator it = std::find(items.begin(), items.end(), someItem);
> if (it != items.end()) {
>     items.erase(it);
> }
> std::sort(items.begin(), items.end());
> // etc
> 
> And now if I decide to exchange vector with list or deque or anything else,
> all I need to do is to change a single line:
> 
> typedef std::list<Item*> Items;
> 
> I don't say that it is the best way to stick with, but it usually works.
> 
>> ight...
>> and what about my function?
>> void WalkListAndDoSomething(List!(T) list);
> 
> Use templates. Or use polymorhism, if you expect containers to implement
> some generic interface.

all I can say is yuk. i can use tricks like that, or i can just use D's
interface. I personally prefer the later.
thanks for the info, though.



More information about the Digitalmars-d mailing list