Konrad Hinsen wrote:
> Maybe I should stress my main worry more clearly: efficiency. I am
> thinking about algorithm steps that are sufficiently fine-grained that
> function call overhead, even more so via pointers (and that's what
> virtual methods come down to), is significant. And I want global
> optimization, with global subexpression elimination etc. for the
> complete algorithm. So it had to happen at the source code level.
Ah, that does make a difference. If run-time polymorphism is too slow,
then you need compile time polymorphism. Todd V. just described the
options well, but I do have a bit to add.
Your stated goal is that you would like to be able to override certain
aspects of an algorithm for different situations. As Todd V. notes, you
can pass a class that encapsulates an algorithm in various ways. Call
that class MyBasicAlgorithm. You could then call a function like:
DoIt(my_data,MyBasicAlgorithm());
The (template) function DoIt might look like:
template<class Alg>
void DoIt(MyData& data, Alg alg)
{
...
alg.crunch(data);
...
}
Now suppose you make a subclass of MyBasicAlgorithm called
MyFancyAlgorithm that overrides crunch (note that crunch need not be
virtual). Then you call:
DoIt(my_data,MyFancyAlgorithm());
Then DoIt uses the new algorithms, which could be inlined because it
knows the complete type of Alg at compile time.
Steve Karmesin
This archive was generated by hypermail 2b29 : Wed Feb 20 2002 - 03:20:05 EST