the crtp and static polymorphism

Discussion in 'C++' started by AdlerSam, Feb 17, 2011.

  1. AdlerSam

    AdlerSam Guest

    Hi,

    I try to understand static polymorphism in the context of the
    curiously recurring template pattern, but fail desperately to catch
    the point.

    Please have a look at the following code:

    #include <iostream>
    using namespace std;
    template <class Derived> struct Base {
    void interface() {
    static_cast<Derived*>(this)->implementation();
    }
    };
    struct Derived1 : Base<Derived1> {
    void implementation() {cout << "Derived1 member" << endl;}
    };
    struct Derived2 : Base<Derived2> {
    void implementation() {cout << "Derived2 member" << endl;}
    };
    int main() {
    Base<Derived1> *x;
    x = new Derived1();
    x->interface();
    // I can't assign a Derived2 instance to x!?
    return 0;
    }

    To me, polymorphism means that it should be possible to assign either
    an instance of Derived1 or Derived2 to the _same_ variable (x in the
    example), and then, when calling a member fuction through x (i.e.
    interface()), having the right implementation being run.

    Can anyone help me showing where I'm off the track?
     
    AdlerSam, Feb 17, 2011
    #1
    1. Advertising

  2. AdlerSam

    sebastian Guest

    On Feb 17, 10:09 am, AdlerSam <> wrote:
    > Hi,
    >
    > I try to understand static polymorphism in the context of the
    > curiously recurring template pattern, but fail desperately to catch
    > the point.
    >
    > Please have a look at the following code:
    >
    > #include <iostream>
    > using namespace std;
    > template <class Derived> struct Base {
    >     void interface() {
    >         static_cast<Derived*>(this)->implementation();
    >     }};
    >
    > struct Derived1 : Base<Derived1> {
    >     void implementation() {cout << "Derived1 member" << endl;}};
    >
    > struct Derived2 : Base<Derived2> {
    >     void implementation() {cout << "Derived2 member" << endl;}};
    >
    > int main() {
    >     Base<Derived1> *x;
    >     x = new Derived1();
    >     x->interface();
    >     // I can't assign a Derived2 instance to x!?
    >     return 0;
    >
    > }
    >
    > To me, polymorphism means that it should be possible to assign either
    > an instance of Derived1 or Derived2 to the _same_ variable (x in the
    > example), and then, when calling a member fuction through x (i.e.
    > interface()), having the right implementation being run.
    >
    > Can anyone help me showing where I'm off the track?


    Simple: Base<Derived1> and Base<Derived2> are not derived from a
    common base, so they are incompatible.
     
    sebastian, Feb 17, 2011
    #2
    1. Advertising

  3. AdlerSam

    AdlerSam Guest

    AdlerSam, Feb 18, 2011
    #3
  4. * AdlerSam, on 18.02.2011 06:22:
    >> Simple: Base<Derived1> and Base<Derived2> are not derived from a
    >> common base, so they are incompatible.

    >
    > Yeah, I know. But that's my problem, as Wikipedia is calling this
    > "static polymorphism":
    >
    > http://en.wikipedia.org/wiki/Template_metaprogramming#Static_polymorphism
    >
    > I just don't understand why!


    Polymorphism is to use the same textual code to handle objects of different types.

    If you placed your call

    x->interface();

    in a templated function, with the type of x as a template parameter, then it
    would be an example of static polymorphism.

    The call

    static_cast<Derived*>(this)->implementation();

    in Base<T>::interface, is an example of static polymorphism.


    Cheers & hth.,

    - Alf

    --
    blog at <url: http://alfps.wordpress.com>
     
    Alf P. Steinbach /Usenet, Feb 18, 2011
    #4
  5. AdlerSam

    AdlerSam Guest

    > Polymorphism is to use the same textual code to handle objects of different types.
    Hmm - ok, with this definition, any template is polymorph, right?

    > If you placed your call
    >
    >    x->interface();
    >
    > in a templated function, with the type of x as a template parameter, then it
    > would be an example of static polymorphism.
    >
    > The call
    >
    >    static_cast<Derived*>(this)->implementation();
    >
    > in Base<T>::interface, is an example of static polymorphism.

    Agreed, with the definition you gave, these are examples of (static)
    polymorphism. But what is so special w.r.t. the CRTP in the context of
    polymorphism then?
     
    AdlerSam, Feb 18, 2011
    #5
  6. AdlerSam

    itaj sherman Guest

    On Feb 18, 9:18 am, AdlerSam <> wrote:
    > > Polymorphism is to use the same textual code to handle objects of different types.

    >


    >
    > Agreed, with the definition you gave, these are examples of (static)
    > polymorphism. But what is so special w.r.t. the CRTP in the context of
    > polymorphism then?


    I'm not even sure how to iterpret "curiously recurring template
    pattern".
    But it does seem that what this pattern does is to enforce common
    interface on groups of classes.
    The template Base<> defines the group, and if you want to write a
    class that belongs to the group, you have to define it:
    class X: Base<X> {...}

    I think this is trying to enforce concepts in the code (maybe that's
    what they call static polymorphism). See boost/concept_check:
    http://www.boost.org/doc/libs/1_45_0/libs/concept_check/concept_check.htm

    itaj
     
    itaj sherman, Feb 18, 2011
    #6
    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. Mike Smith

    ABC vs. CRTP?

    Mike Smith, Mar 2, 2005, in forum: C++
    Replies:
    7
    Views:
    949
    Dietmar Kuehl
    Mar 3, 2005
  2. fabioppp

    CRTP question

    fabioppp, Apr 15, 2005, in forum: C++
    Replies:
    2
    Views:
    400
    James Aguilar
    Apr 15, 2005
  3. Krivenok Dmitry
    Replies:
    13
    Views:
    1,492
    Axter
    Jun 1, 2006
  4. CRTP and Factories

    , Jul 25, 2006, in forum: C++
    Replies:
    2
    Views:
    348
  5. Hicham Mouline
    Replies:
    8
    Views:
    1,049
    Hicham Mouline
    Mar 27, 2009
Loading...

Share This Page