Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

how would one define/use polymorphic implementations of STL types ??

0 views
Skip to first unread message

Coder Fodder

unread,
Feb 4, 2010, 9:01:51 AM2/4/10
to
Hello there.

I have been pondering how one would do such a thing.

For example, the std::set type uses a red-black tree impl.

If I decided that I needed a special impl for set for some
reason, how would one do this ??

So we might have :


// Some function that should be applicable to any impl of set

void doSomething( ?1? theSet)
{
?1?::iterator iter = theSet.begin() ;
// ...
}

std::set<int> s1 ; // the standard STL impl
?2? s2 ; // the special impl of set

doSomething(s1) ;
doSomething(s2) ;

How would ?1? and ?2? be defined and/or implemented ??

Or is the above just not possible (you are stuck with one
impl of every STL type in every program - whatever that may be) ??


Regards

Gert-Jan de Vos

unread,
Feb 4, 2010, 10:40:08 AM2/4/10
to
On Feb 4, 3:01 pm, Coder Fodder <nos...@my.com> wrote:
> Hello there.
>
> I have been pondering how one would do such a thing.
>
> For example, the std::set type uses a red-black tree impl.
>
> If I decided that I needed a special impl for set for some
> reason, how would one do this ??
>
> So we might have :
>
> // Some function that should be applicable to any impl of set
>
> void doSomething( ?1? theSet)
> {
>     ?1?::iterator iter = theSet.begin() ;
>     // ...
> }

template <typename SetType>
void doSomething(const SetType& theSet)
{
SetType::iterator iter = theSet.begin();
// ...
}

> std::set<int> s1 ; // the standard STL impl
> ?2? s2 ; // the special impl of set

MySet<int> s2;

> doSomething(s1);
> doSomething(s2);

This is exactly how things like std::sort can work on
e.g. either std::queue<T> or std::vector<T> or built-in
arrays.

Thomas J. Gritzan

unread,
Feb 4, 2010, 12:21:55 PM2/4/10
to
Am 04.02.2010 16:40, schrieb Gert-Jan de Vos:
> On Feb 4, 3:01 pm, Coder Fodder <nos...@my.com> wrote:
>> I have been pondering how one would do such a thing.
>>
>> For example, the std::set type uses a red-black tree impl.
>>
>> If I decided that I needed a special impl for set for some
>> reason, how would one do this ??
>>
>> So we might have :
>>
>> // Some function that should be applicable to any impl of set
>>
>> void doSomething( ?1? theSet)
>> {
>> ?1?::iterator iter = theSet.begin() ;
>> // ...
>> }
>
> template <typename SetType>
> void doSomething(const SetType& theSet)
> {
> SetType::iterator iter = theSet.begin();

<nitpicking>
typename SetType::const_iterator iter = theSet.begin();
</nitpicking>

> // ...
> }

--
Thomas

0 new messages