When to use CRTP ?

Discussion in 'C++' started by mathieu, Oct 15, 2009.

  1. mathieu

    mathieu Guest

    Hi there,

    I am reading the following post:

    http://groups.google.com/group/comp.lang.c /browse_thread/thread/2805ab0a765aa0ad

    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
    mathieu, Oct 15, 2009
    #1
    1. Advertising

  2. mathieu

    SG Guest

    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
    SG, Oct 15, 2009
    #2
    1. Advertising

  3. * mathieu:
    > Hi there,
    >
    > I am reading the following post:
    >
    > http://groups.google.com/group/comp.lang.c /browse_thread/thread/2805ab0a765aa0ad
    >
    > 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
    Alf P. Steinbach, Oct 15, 2009
    #3
    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:
    898
    Dietmar Kuehl
    Mar 3, 2005
  2. fabioppp

    CRTP question

    fabioppp, Apr 15, 2005, in forum: C++
    Replies:
    2
    Views:
    373
    James Aguilar
    Apr 15, 2005
  3. CRTP question

    , Apr 17, 2006, in forum: C++
    Replies:
    2
    Views:
    385
  4. CRTP and Factories

    , Jul 25, 2006, in forum: C++
    Replies:
    2
    Views:
    323
  5. Noah Roberts

    CRTP

    Noah Roberts, Jan 12, 2007, in forum: C++
    Replies:
    6
    Views:
    880
    kwikius
    Jan 15, 2007
Loading...

Share This Page