03.12.2012 Views

C++ for Scientists - Technische Universität Dresden

C++ for Scientists - Technische Universität Dresden

C++ for Scientists - Technische Universität Dresden

SHOW MORE
SHOW LESS

Create successful ePaper yourself

Turn your PDF publications into a flip-book with our unique Google optimized e-Paper software.

5.2. PROVIDING TYPE INFORMATION 135<br />

This requires the evaluation of fibonacci::value as<br />

template <br />

struct fibonacci<br />

{<br />

static const long value= 0 < 3 ? 1 : fibonacci< −1>::value + fibonacci< −2>::value; // error<br />

};<br />

which needs fibonacci< −1>::value . . . . Although the values <strong>for</strong> N < 3 are not used in the end,<br />

the compiler will nevertheless generate these terms infinitely and die at some point.<br />

We said be<strong>for</strong>e that we implement the computation recursively. In fact, all repetive calculations<br />

must be realized recursively as there is no iteration <strong>for</strong> 2 meta-functions.<br />

If we write <strong>for</strong> instance<br />

std::cout ≪ fibonacci::value ≪ ”\n”;<br />

the value would be already calculated during the compilation and the program just prints<br />

it. If you do not believe us, you can read the assembler code (e.g. compile with ‘g++ -S<br />

fibonacci.cpp -o fibonacci.asm’).<br />

We mentioned long compilations with meta-programming at the beginning of the chapter. The<br />

compilation <strong>for</strong> Fibonacci number 45 took less than a second. Compared to it, a naïve run-time<br />

implemtation:<br />

long fibonacci2(long x)<br />

{<br />

return x < 3 ? 1 : fibonacci2(x−1) + fibonacci2(x−2);<br />

}<br />

took 14s on the same computer. The reason is that the compiler remember intermediate results<br />

while the run-time version recomputes everything. We are, however, convinced that every reader<br />

of this book can rewrite fibonacci2 without the exponential overhead of recomputations.<br />

5.2 Providing Type In<strong>for</strong>mation<br />

5.2.1 Type Traits<br />

When we write template functions, we can easily define temporary values because they have<br />

usually the same type as one of the template arguments. But not always. Imagine you have a<br />

function that returns from two value that with the minimal magnitude:<br />

template <br />

T inline min magnitude(const T& x, const T& y)<br />

{<br />

using std::abs;<br />

T ax= abs(x), ay= abs(y);<br />

return ax < ay ? x : y;<br />

}<br />

We can call this <strong>for</strong> int, unsigned, double values:<br />

2 The Meta Programming Library provides compile-time iterators but even those are recursive internally.

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!