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

C

Coder Fodder

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
 
G

Gert-Jan de Vos

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 said:
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.
 
T

Thomas J. Gritzan

Am 04.02.2010 16:40, schrieb Gert-Jan de Vos:
template <typename SetType>
void doSomething(const SetType& theSet)
{
SetType::iterator iter = theSet.begin();

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

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,733
Messages
2,569,439
Members
44,829
Latest member
PIXThurman

Latest Threads

Top