Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C++ > When to use CRTP ?

Reply
Thread Tools

When to use CRTP ?

 
 
mathieu
Guest
Posts: n/a
 
      10-15-2009
Hi there,

I am reading the following post:

http://groups.google.com/group/comp....05ab0a765aa0ad

And I do not understand the following:

....
- static polymorphism which allows the inheritance or cooperation of
traits and useful specialization under metaprogramming (factorizing
operations by example).
....

So as in the original post, I do not understand what is the added
value of CRTP. Could someone please provide an example where
inheritance of traits clearly requires CRTP, thanks !

-Mathieu
 
Reply With Quote
 
 
 
 
SG
Guest
Posts: n/a
 
      10-15-2009
mathieu wrote:
> * So as in the original post, I do not understand what is the added
> value of CRTP.


See the Wikipedia article. It contains some examples. The Barton-
Nackman-trick is another example for a CRTP application with its own
Wikipedia article. (Boost.Operators use this trick as far as I can
tell)

Boost.uBLAS also uses CRTP, although, I'm not 100% sure about how and
why. Maybe someone knowledgable here can shed some more light on what
Boost.uBLAS actually does w.r.t. CRTP.

Cheers,
SG
 
Reply With Quote
 
 
 
 
Alf P. Steinbach
Guest
Posts: n/a
 
      10-15-2009
* mathieu:
> Hi there,
>
> I am reading the following post:
>
> http://groups.google.com/group/comp....05ab0a765aa0ad
>
> And I do not understand the following:
>
> ...
> - static polymorphism which allows the inheritance or cooperation of
> traits and useful specialization under metaprogramming (factorizing
> operations by example).
> ...
>
> So as in the original post, I do not understand what is the added
> value of CRTP. Could someone please provide an example where
> inheritance of traits clearly requires CRTP, thanks !


I've got a headache so I'm not going to look up the original thread.

But here's an example of CRTP in action, off the cuff (may have syntax erors):

#include <iostream>

using namespace std;

template< class WorkerKind >
struct Worker
{
void sayHello() const
{
WorkerKind const& self = *static_cast<WorkerKind const*>( this );
cout << "Hello, I'm a " << self.kind() << "!" << endl;
}
};

struct Electrician: Worker<Electrician>
{
char const* kind() const { return "electrician"; }
};

struct Plumber: Worker<Plumber>
{
char const* kind() const { return "plumber"; }
}

int main()
{
Plumber().sayHello();
Electrician().sayHello();
}

Oh well it's not inheritance of "traits", didn't see that.

Cheers & hth.,

- Alf
 
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