Link to home
Start Free TrialLog in
Avatar of shriv
shriv

asked on

C++ templates

I'm looking for some information or discussion on some of
more odd uses of C++ templates, particularly of the forms

     class A : public B<A>

     template<class B>
     class D : public B

and similar such forms where you are templatizing some
aspect of an inherhitance hierarchy, and introducing what
looks like strange coupling between base and derived
classes.


I've tried, without luck, to find J. Coplien's C++ report
article on Curiously Recurring Template Patterns. I'm
looking for insight on how these constucts are used,
how they are useful, etc.

Hoping there's an expert out there that can enlighten me!

Thanks!

--shri
ASKER CERTIFIED SOLUTION
Avatar of yonat
yonat

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of yonat
yonat

It was the February 1995 issue, and is reprinted in the book "C++ Gems".
Avatar of shriv

ASKER

I appreciate the pointer to the article. I think, though, that the implications of these template
forms are more subtle than the explanation. I understand that templates are used for
type saftey & reuse: this is the most common usage in collection classes. The "deriving
from itself" appears to be something else entirely, and I believe, has uses beyond
just the singleton example, uses which I'm still unable to grok at this point.

I'd also appreciate an expansion on the adapter pattern implementation.


Here is another example of class A : public B<A>: Let's say you want to keep track of all instances of a class. Then you write:

template <typename T>
class TypeTracker
{
        static std::List<TypeTracker *> theListOfAllT;
public:
        TypeTracker()  { theListOfAllT.push_back(this); }
        ~TypeTracker() { theListOfAllT.erase(this); }
        staic const std::List<TypeTracker *> &getAll() { return theListOfAll; }
};
TypeTracker::theListOfAll;

class MyTrackedClass : virtual public TypeTracker<MyTrackedClass>
{
        ...
};

As for the adapter, see more info at http://rampages.onramp.net/~huston/adapter.html and http://rampages.onramp.net/~huston/adapter.cpp .