Options: Use Forum View Use Monospaced Font Show Text Part by Default Condense Mail Headers Message: [<< First] [< Prev] [Next >] [Last >>] Topic: [<< First] [< Prev] [Next >] [Last >>] Author: [<< First] [< Prev] [Next >] [Last >>]

```Team members,
A friend and past colleague (Jim McKelvey, project scientist) at JPL finds
will
let him speak for himself. This topic overlaps Ada promotion and Ada
--Ken Fowler, MITRE

"C++ is a lot of fun, even though I would probably push Ada-95. The

> C++ "templates" are much more powerful than Ada generics, unless
> Ada-95 has beefed them up. (Are you familiar with the Blitz project?
> They use templates in a  radically new way. It  might be usable with
>
To describe template expressions (as in Blitz), let's make up a
scenario. Suppose we want a class to do matrix operations, and we

A = B + C + D;

to add some 3x4 matrices.  What really happens is:

Create and initialize a temporary (say X)
Set it to the value of B + C
Create and initialize a temporary (say Y)
Set it to the value of X + D
Assign the value of Y to A
Destroy X and Y

The overhead is horrendous, and it's difficult for an optimizer to
do
much to help.

We can recast it as procedure calls:

which is faster. But we have lost our nice notation, and still need
a
temporary.

We could do:

with a little more work.

In Blitz, though, the matrix expression A = B + C + D
would construct code to compute A = B + C + D as an
expression. In other words, the generated code would
look more like this:

for (i = 0; i < 3; i++)
{
for (j = 0; j < 4; i++)
{
A[i][j] = B[i][j] + C[i][j] + D[i][j];
}
}

No temporaries - and the more complicated the expression, the
greater the speedup. In fact, we could even unroll the loops for a
greater speedup. The situation is readily understood by an optimizer.

How does it do THAT you ask? By aggressively applying two
techniques:

Recursive template instantiation (mostly implicitly)
function Inlining

In fact, C++ is moving in the direction of extensive use of
templates,      even in situations where they are not expected. The new
library is
the Standard Template Library after all.

The major down side to all this is that not one programmer in a
thousand will ever really understand how a package like Blitz works,    and
not one programmer in a million will ever undertake a project
using template expressions.

The real work at runtime is done in Blitz methods that are not
normally inlined. So there is no great increase in memory.
Here's a C++ example to try:

#include <iostream.h>

// Template
template <const unsigned long int n>

struct Factorial
{
static const unsigned long int factorial =
n * Factorial<n - 1>::factorial;
};

// Specialization of template
struct Factorial<2>
{
static const unsigned long int factorial = 2;
};

// Specialization of template
struct Factorial<1>
{
static const unsigned long int factorial = 1;
};

// Specialization of template
struct Factorial<0>
{
static const unsigned long int factorial = 1;
};

int main(void)
{
cout << Factorial<6>::factorial << endl;

return 1;
}

Compile this with full optimization and look at the generated code!
Can this be translated into Ada95? The implicit instantiations can
be made explicit -- the kicker is the specialization to end the
recursion.

Note that you cannot end the recursion (at least in C++) by
changing the main expression to something like:

(n > 1) ? (n * Factorial<n - 1>::factorial) : 1

because the expression is not evaluated until after template
expansion. Instead you'll bomb out by exceeding the template
recursion depth."

--Jim
```