Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C++ > What's polymorphic in CRTP

Reply
Thread Tools

What's polymorphic in CRTP

 
 
Öö Tiib
Guest
Posts: n/a
 
      02-15-2013
On Friday, 15 February 2013 18:55:12 UTC+2, Cholo Lennon wrote:
> On 02/15/2013 01:33 PM, Öö Tiib wrote:
> > On Friday, 15 February 2013 18:18:47 UTC+2, Cholo Lennon wrote:
> >> How about this?

> >
> > It is example that works but does not achieve much else but inefficiency.
> >

>
> Why the example is inefficient? The call to Derived:nStatic is
> resolved at compile time.


Because there are virtual functions (more expensive) that can not be used for
dynamic polymorphism (purpose is lost).
I explained it further. In my post that you quoted but apparently did not read.
 
Reply With Quote
 
 
 
 
Cholo Lennon
Guest
Posts: n/a
 
      02-15-2013
On 02/15/2013 02:05 PM, Öö Tiib wrote:
> On Friday, 15 February 2013 18:55:12 UTC+2, Cholo Lennon wrote:
>> On 02/15/2013 01:33 PM, Öö Tiib wrote:
>>> On Friday, 15 February 2013 18:18:47 UTC+2, Cholo Lennon wrote:
>>>> How about this?
>>>
>>> It is example that works but does not achieve much else but inefficiency.
>>>

>>
>> Why the example is inefficient? The call to Derived:nStatic is
>> resolved at compile time.

>
> Because there are virtual functions (more expensive) that can not be used for
> dynamic polymorphism (purpose is lost).
> I explained it further. In my post that you quoted but apparently did not read.
>


and... have you read my explanation? This was an *example*, not a
production code. The idea was to show the dynamic and static variant in
a simple way.

Regards


--
Cholo Lennon
Bs.As.
ARG
 
Reply With Quote
 
 
 
 
Öö Tiib
Guest
Posts: n/a
 
      02-15-2013
On Friday, 15 February 2013 19:25:10 UTC+2, Cholo Lennon wrote:
> On 02/15/2013 02:05 PM, Öö Tiib wrote:
> > On Friday, 15 February 2013 18:55:12 UTC+2, Cholo Lennon wrote:
> >> On 02/15/2013 01:33 PM, Öö Tiib wrote:
> >>> On Friday, 15 February 2013 18:18:47 UTC+2, Cholo Lennon wrote:
> >>>> How about this?
> >>>
> >>> It is example that works but does not achieve much else but inefficiency.
> >>>
> >>
> >> Why the example is inefficient? The call to Derived:nStatic is
> >> resolved at compile time.

> >
> > Because there are virtual functions (more expensive) that can not be used for
> > dynamic polymorphism (purpose is lost).
> > I explained it further. In my post that you quoted but apparently did not read.

>
> and... have you read my explanation? This was an *example*, not a
> production code. The idea was to show the dynamic and static variant in
> a simple way.


I do not, yes, understand something here. Can you elaborate? Example must
be ideally simplest that works. Simpler than that is a lie, not example.
Someone may see it and think that it is showing how to mix dynamic and static
polymorphism, but it can't. What production code? I was saying that it should
be example on "never do like that" page with large red cross on it.
 
Reply With Quote
 
Cholo Lennon
Guest
Posts: n/a
 
      02-15-2013
On 02/15/2013 03:03 PM, Öö Tiib wrote:
> On Friday, 15 February 2013 19:25:10 UTC+2, Cholo Lennon wrote:
>> On 02/15/2013 02:05 PM, Öö Tiib wrote:
>>> On Friday, 15 February 2013 18:55:12 UTC+2, Cholo Lennon wrote:
>>>> On 02/15/2013 01:33 PM, Öö Tiib wrote:
>>>>> On Friday, 15 February 2013 18:18:47 UTC+2, Cholo Lennon wrote:
>>>>>> How about this?
>>>>>
>>>>> It is example that works but does not achieve much else but inefficiency.
>>>>>
>>>>
>>>> Why the example is inefficient? The call to Derived:nStatic is
>>>> resolved at compile time.
>>>
>>> Because there are virtual functions (more expensive) that can not be used for
>>> dynamic polymorphism (purpose is lost).
>>> I explained it further. In my post that you quoted but apparently did not read.

>>
>> and... have you read my explanation? This was an *example*, not a
>> production code. The idea was to show the dynamic and static variant in
>> a simple way.

>
> I do not, yes, understand something here. Can you elaborate? Example must
> be ideally simplest that works. Simpler than that is a lie, not example.
> Someone may see it and think that it is showing how to mix dynamic and static
> polymorphism, but it can't. What production code? I was saying that it should
> be example on "never do like that" page with large red cross on it.
>


OMG, the OP asked for an example of static polymorphism with CRTP and
you gave him an example of static polymorphism with templates but
without CRTP! Nice way to teach the concept!

Just in case you insist with my example, I re-wrote it for you:


#include <iostream>

namespace dynamic_polymorphism
{
class Base {
public:
void callFoo()
{
onFoo();
}

virtual void onFoo()
{
std::cout << "Base:nFoo" << std::endl;
}

};

class Derived: public Base {
public:
virtual void onFoo()
{
std::cout << "Derived:nFoo" << std::endl;
}

};
}


namespace static_polymorphism
{
template<class T>
class Base {
public:
void callFoo()
{
T* pThis = static_cast<T*>(this);
pThis->onFoo();
}

void onFoo()
{
std::cout << "Base:nFoo" << std::endl;
}

};

class Derived: public Base<Derived> {
public:
// "pseudo virtual" function
void onFoo()
{
std::cout << "Derived:nFoo" << std::endl;
}

};
}


int main(int, char**)
{
dynamic_polymorphism:erived der1;
der1.callFoo();

static_polymorphism:erived der2;
der2.callFoo();
}


Are you happy now?

Regards


--
Cholo Lennon
Bs.As.
ARG


 
Reply With Quote
 
Öö Tiib
Guest
Posts: n/a
 
      02-16-2013
On Saturday, 16 February 2013 00:18:44 UTC+2, Cholo Lennon wrote:
> On 02/15/2013 03:03 PM, Öö Tiib wrote:
> > On Friday, 15 February 2013 19:25:10 UTC+2, Cholo Lennon wrote:
> >> On 02/15/2013 02:05 PM, Öö Tiib wrote:
> >>> On Friday, 15 February 2013 18:55:12 UTC+2, Cholo Lennon wrote:
> >>>> On 02/15/2013 01:33 PM, Öö Tiib wrote:
> >>>>> On Friday, 15 February 2013 18:18:47 UTC+2, Cholo Lennon wrote:
> >>>>>> How about this?
> >>>>>
> >>>>> It is example that works but does not achieve much else but inefficiency.
> >>>>>
> >>>>
> >>>> Why the example is inefficient? The call to Derived:nStatic is
> >>>> resolved at compile time.
> >>>
> >>> Because there are virtual functions (more expensive) that can not be used for
> >>> dynamic polymorphism (purpose is lost).
> >>> I explained it further. In my post that you quoted but apparently didnot read.
> >>
> >> and... have you read my explanation? This was an *example*, not a
> >> production code. The idea was to show the dynamic and static variant in
> >> a simple way.

> >
> > I do not, yes, understand something here. Can you elaborate? Example must
> > be ideally simplest that works. Simpler than that is a lie, not example..
> > Someone may see it and think that it is showing how to mix dynamic and static
> > polymorphism, but it can't. What production code? I was saying that it should
> > be example on "never do like that" page with large red cross on it.
> >

>
> OMG, the OP asked for an example of static polymorphism with CRTP and
> you gave him an example of static polymorphism with templates but
> without CRTP! Nice way to teach the concept!


The concept we discussed before. CRTP is just one pattern for to use static
polymorphism, but that I said to him in my first reply already.

Now he was asking what to do with the resulting with CRTP functions that
have same signature (he said are "similar") but do not belong to same virtual
hierarchy (he said are not "same"). So I said that he should continue using
static polymorphism.

> Just in case you insist with my example, I re-wrote it for you:


Thanks.

> Are you happy now?


Sometimes people see me more grumpy than I really am. I try to point out the
flaws to be helpful. Everything contains mistakes, mine code too, yours was
not making me unhappy or anything.

Mixing static and dynamic polymorphism may be needed in same application.
For example if you need a container that contains polymorphic pointers then
there you have to use dynamic polymorphism, static does not work.

 
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 and Factories alexander.stippler@uni-ulm.de C++ 2 07-25-2006 02:35 PM
CRTP question michael.alexeev@qwest.com C++ 2 04-17-2006 06:37 PM
CRTP question fabioppp C++ 2 04-15-2005 04:43 PM
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