March 29, 2007

GPLv3 draft. C++ CRTP

A new discussion draft of the GNU GPL has been released. Go here to see more. Note the end of the Patents section in the changes guide:

specifically granted to recipients of the covered work under this License[, unless you entered into that arrangement, or that patent license was granted, prior to March 28, 2007].

I heard for first time about the Curiously Recurring Template Pattern when I read the Eigen library webpage. Now I found this example by Bruce Eckel that makes it much clearer:

//: generics/Mixins.cpp

#include

#include

#include

using namespace std;

template<class T> class TimeStamped : public T {

  long timeStamp;

public:

  TimeStamped() { timeStamp = time(0); }

  long getStamp() { return timeStamp; }

};

template<class T> class SerialNumbered : public T {

  long serialNumber;

  static long counter;

public:

  SerialNumbered() { serialNumber = counter++; }

  long getSerialNumber() { return serialNumber; }

};

// Define and initialize the static storage:

template<class T> long SerialNumbered<T>::counter = 1;

int main() {

  TimeStamped<SerialNumbered<string> > mixin1, mixin2;

  mixin1.append(“test string 1”); // A string method

  mixin2.append(“test string 2”);

  cout << mixin1 << “ “ << mixin1.getStamp() << “ “ <<

    mixin1.getSerialNumber() << endl;

  cout << mixin2 << “ “ << mixin2.getStamp() << “ “ <<

    mixin2.getSerialNumber() << endl;

}

The effect you get is basically what you get with Ruby Mixins.