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

Discussion in 'C++' started by Coder Fodder, Feb 4, 2010.

  1. Coder Fodder

    Coder Fodder Guest

    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
     
    Coder Fodder, Feb 4, 2010
    #1
    1. Advertising

  2. On Feb 4, 3:01 pm, Coder Fodder <> 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.
     
    Gert-Jan de Vos, Feb 4, 2010
    #2
    1. Advertising

  3. Am 04.02.2010 16:40, schrieb Gert-Jan de Vos:
    > On Feb 4, 3:01 pm, Coder Fodder <> 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
     
    Thomas J. Gritzan, Feb 4, 2010
    #3
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Aryeh M. Friedman
    Replies:
    2
    Views:
    1,138
    Ron Natalie
    Feb 21, 2005
  2. pmatos
    Replies:
    5
    Views:
    352
    Marcin Kalicinski
    Mar 11, 2005
  3. pmatos
    Replies:
    3
    Views:
    369
    Victor Bazarov
    Mar 24, 2005
  4. Karel Miklav
    Replies:
    9
    Views:
    379
    red floyd
    Sep 6, 2005
  5. A. W. Dunstan
    Replies:
    2
    Views:
    353
    P.J. Plauger
    May 10, 2006
Loading...

Share This Page