[LC++]Generic programming vs OO inheritance

Mark Phillips mark at austrics.com.au
Thu Aug 16 15:37:06 UTC 2001


Dan hursh wrote:

> I think I'll also disagree with the statement "that with generic 
> programming it would seem that the compiler can't do error checking 
> nearly as well."

You've almost convinced me on this point.  Template "typename" (or "class") parameters give no information about the type of class which may be passed in.  This is in
contrast to the OO base-class approach where the base-class effectively defines the type of class that pointers to this base-class are allowed to represent.  This is why I
thought error checking would be 
inherently worse with generic programming.  I still wonder whether there
would be cases where this is true, but I think you are right that in the
main it is not the case.  The "meta type" of template "typename" parameters
is never defined explicitly, but implicitly it is, by use.  If somewhere
the parameter is used as if it has a member function ".get()" for example,
then this is implicitly saying that only classes which have this 
member function get() may be used as a parameter.  The compiler should
be able to detect this kind of error at compile time.  Indeed it does,
though the error messages are fairly ugly.

Because the programmer does not have to _explicitly_ define the 
"meta type" of "typename" parameters, it is certainly easier for them
to make logical errors.  I wonder whether there might be clear examples
of where generic programming is more error prone than OO, but I can't
think of any.


>         As far as the functional equivalence of generic programming and OO
> inheritance, I don't think you can get the runtime behavior of OO
> inheritance with gernerics without also use base-classes or implementing
> you're own vtable-like structures.

I suspect that they are not completely equivalent.  What is true I think, 
is that often the OO approach does at runtime, things that could be done at
compile time.

I would be interested to hear an example of something which could be done
with OO inheritance that can't be done with generic programming.  At the
moment I can't think of any, but they may well exist.  I am guessing that
you would need some kind of recursive definitions or something.

OO is mostly concerned with giving data individualized functionality: 
member functions.  Because C++ does not allow self modifying code (in the
strict sense --- templates allow the easy production of multiple code 
variants), all functions will be determined at compile time.  The OO 
base-class chooses between variant member functions at run-time, when
in actual fact this information is available at compile time.  That is
why the generic programming approach is superior in essence.

I don't think the full power of templates was realized when they were
added to the language.  Because of this I think template implementations
are a bit messy and clumsy.  However I suspect that before too long 
someone will design a language with similar ideas, but expressed much
more cleanly.  It seems to me that functional programming languages
are not too far away from this.

Cheers,

Mark.



More information about the tuxCPProgramming mailing list