the crtp and static polymorphism

A

AdlerSam

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?
 
S

sebastian

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.
 
A

Alf P. Steinbach /Usenet

* AdlerSam, on 18.02.2011 06:22:
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
 
A

AdlerSam

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?
 
I

itaj sherman

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
 

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

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,579
Members
45,053
Latest member
BrodieSola

Latest Threads

Top