Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C++ > the crtp and static polymorphism

Reply
Thread Tools

the crtp and static polymorphism

 
 
AdlerSam
Guest
Posts: n/a
 
      02-17-2011
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?
 
Reply With Quote
 
 
 
 
sebastian
Guest
Posts: n/a
 
      02-17-2011
On Feb 17, 10:09*am, AdlerSam <(E-Mail Removed)> 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.
 
Reply With Quote
 
 
 
 
AdlerSam
Guest
Posts: n/a
 
      02-18-2011
> 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/Templat...c_polymorphism

I just don't understand why!
 
Reply With Quote
 
Alf P. Steinbach /Usenet
Guest
Posts: n/a
 
      02-18-2011
* 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/Templat...c_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>
 
Reply With Quote
 
AdlerSam
Guest
Posts: n/a
 
      02-18-2011
> 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?
 
Reply With Quote
 
itaj sherman
Guest
Posts: n/a
 
      02-18-2011
On Feb 18, 9:18 am, AdlerSam <(E-Mail Removed)> 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...cept_check.htm

itaj
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
CRTP, member typedefs and access control Hicham Mouline C++ 8 03-27-2009 09:53 AM
CRTP and Factories alexander.stippler@uni-ulm.de C++ 2 07-25-2006 02:35 PM
Dynamic polymorphism vs. Static polymorphism Krivenok Dmitry C++ 13 06-01-2006 09:49 AM
HSSI & cRTP Arthur Lashin Cisco 0 03-19-2005 08:19 AM
ABC vs. CRTP? Mike Smith C++ 7 03-03-2005 05:43 PM



Advertisments