TITLE: one kind of constrained genericity [ Constrained genericity is a topic discussed in detail in the book "Object Oriented Software Construction", by Bertrand Meyer. When you here "generic", you, as a C++ developer, should think of templates. The "constrained" part deals with specifying restriction or assumptions about the types and arguments supplied to the generic (template). Here is an example of a way to express and ENFORCE AT COMPILE TIME, the fact that a given class must be derived from some other class. Specifically, class K must be derived from HashTable. - adc ] PROBLEM: billf@medicus.com (Bill Foote) There is a kludgearound for this in C++: In the template implementation, you could have a method like this: template void HashTable::dummy() { K* k = NULL; Hashable* h = k; // If this fails to compile, it's because // K is not derived from Hashable. } so you *can* force type-conformance of template parameters. It's pretty ugly, though, and I doubt that many people do this sort of thing. RESPONSE: matt@godzilla.EECS.Berkeley.EDU That's an interesting trick! I hadn't heard about it before. This trick is a bit of a kludge, and I doubt that most programmers will end up using it, but I hope that library implementors will take notice. It's nice to see that there is a way of enforcing constrained genericity in C++. (But, of course, it's not a real substitute for constrained genericity as a language feature.) RESPONSE: osinski@valis.cs.nyu.edu (Ed Osinski), 5 Jul 95 Actually, the trick is a bit more work than that, I think. Isn't it true that template member functions that are not used aren't supposed to be instantiated, according to the standard? It seems that in order to absolutely guarantee that the constraint is checked, we need to guarantee that "dummy" gets called no matter what is done with the object. One way to do this is to call "dummy" in all constructors of HashTable (including ones generated by the compiler!) Something like this would help: template class SubClass { public: SubClass () { D *d = NULL; B *b = d; } }; #define CONSTRAINT template class HashTable : CONSTRAINT SubClass { ... }; This has the advantage of having the constraint visible at the beginning of the declaration for HashTable.