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

You also want an ePaper? Increase the reach of your titles

YUMPU automatically turns print PDFs into web optimized ePapers that Google loves.

4.6. TEMPLATE SPECIALIZATION 105<br />

Base inline power(const Base& x, int y);<br />

template <br />

double inline power(double x, const Exponent& y);<br />

The compiler will find all overloads that match the argument combination and select the most<br />

specific. For instance, power(3.0, 2u) will match <strong>for</strong> the first and third overload where the latter<br />

is more specific. 11 To put it to higher math: 12 type specificity is a partial order that <strong>for</strong>ms a<br />

lattice and the compiler picks the maximum of the available overloads. However, you do not<br />

need to dive deeply into algebra to see which type or type combination is more specific.<br />

If we call power(3.0, 2) with the previous overloads all three matches. However, this time we<br />

cannot determine the most specific overload. The compiler will tell us that the call is ambiguous<br />

and show us overload 2 and 3 as candidates. As we implemented the overloads consisently and<br />

with optimal per<strong>for</strong>mance we might be glad with either choice but the compiler will not choose.<br />

To disambiguate the overloads we must add:<br />

double inline power(double x, int y);<br />

The lattice people from the previous paragraph will think “Of course, we were missing the join<br />

in the specificity order.” Again, one can understand C ++ without studying lattices.<br />

4.6.3 Partial Specialization<br />

If you implemented template classes you will run sooner or later in the situation where you like<br />

to specialize a template class <strong>for</strong> another template class. Suppose we have a templated complex<br />

class:<br />

template <br />

class complex;<br />

Assume further that we had some really boosting algorithmic specialization <strong>for</strong> complex vectors<br />

13 that safes tremendous compute time. Then we start specializing our vector class:<br />

template <br />

class vector;<br />

template <br />

class vector; // again ??? :−/<br />

template <br />

class vector; // how many more ??? :−P<br />

Apparently, this lacks elegance to reimplement the specialization <strong>for</strong> all possible and impossible<br />

instantiations of complex. Much worse, it destroys our ideal of universal applicability because<br />

the complex class is intended to support user-defined types as Real but the specialization of the<br />

vector class will be ignored <strong>for</strong> those types.<br />

The solution to the implementation redundancy and the ignorance of new types is ‘Partial<br />

Specialization’. We specialize our vector class <strong>for</strong> all complex instantiations:<br />

11 TODO: Exercises <strong>for</strong> which type is more specific than which.<br />

12 For those who like higher mathematics. And only <strong>for</strong> those.<br />

13 TODO: Anyone a good example?

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

Saved successfully!

Ooh no, something went wrong!