Study of C++
Multiple Inheritance - the fantastic and the horrible
- Multiple inheritance is great: it simultaneously gives you
- Interfaces for free: an interface is an abstract class will null methods.
- A form of Mixin
classes (fancier example: Mixin
classes in C++)
- (mixins are an idea originating in CLOS, the Common Lisp Object System)
-- we will discuss both of these briefly.
- The major technical hiccup is The Diamond
Problem. Blech! We will briefly review the C++ solutions to this
problem, including virtual inheritance.
RTTI is RunTime Type Infomation.
- This is a simple concept, objects carry their originating class
names around at runtime.
- (Recall that declared types and runtime types may differ in
languages with subtyping: the static type there means "this type or a
subclass / implementing class")
- In Java this always happens due to its need to ensure safety of
downcasts (raise an exception if they are bad).
- The downside is there is extra runtime overhead on adding this
information to objects.
- In C++ the runtime type information is only added by the
compiler if you need it (you are using RTTI on that object).
We will briefly look at some of the code linked above to see how it
is done in practice.
- The C++ answer to parametric polyorphism / generics
- Unlike other parametric types, templates are really just a
fancier macro mechanism for C++
- Templates are all instantiated by a preprocessor at compile-time
with the actual types, then that expanded code is what is compiled
- There is no need for fancy bounded subtyping like in Java -
just instantiate and compile; the final code may have a type
error due to a subtyping error
- The latter point makes it hard to debug type errors in
templates -- you need to explicitly think about staged
compilation (see below) to understand what the compiler is doing!
- But, Java generics get really complicated as well due to all of
the sub-sub-sub cases and various needed extensions.
Template metaprogramming in C++ is part of a larger topic: staged
computation.
- Partial evaluation: evaluate some of the program code at compile
time, e.g. apply functions if (enough) arguments are known at compile-time.
This is a whole subfield of research.
- MetaML
is a cleaner and more general notion than C++ template
metaprogramming - code is first-class, can
be combined almost like strings, but in a well-typed way.
- Run the first stage, which can do arbitrary computations;
the result returned is the second-stage program.
- run the second stage, returning third stage program.
- etc - run until the final program results.
- ... then run that to do the actual application task.
Observe
- In terms of this model, C++ template metaprogramming is a 2-stage process.
- Also the default is reversed: unlabelled C++ code is
analogous to the <bracketed> code in MetaML since its
running is delayed.
We will go through the C++ example above.
Another handy feature of templates, it allows implementations to be
specialized at instantiation time.
an
example.
The overloading debate is longstanding.
- Overloading is great: such compact syntax with such great power!
- Overloading is horrible: I have no idea which operators were
overloaded where so I don't know what the heck this program is doing!
In general this topic is part of the debate of minimal vs maximal
feature sets for languages.
"How many handy dandy special cases should be supported by FaveLang?"
- LOTS! - but, now you have to worry about all of the contentious
overlap between fancy feature 1 and fancy feature 2.
- LITTLE! - but, now you have to write more code for various
patterns, over and over and over...
Overloading done right in Haskell.
The above webpage has good examples.
Last modified: Fri Apr 25 13:20:38 EDT 2008