C++ for Scientists - Technische Universität Dresden
C++ for Scientists - Technische Universität Dresden
C++ for Scientists - Technische Universität Dresden
Create successful ePaper yourself
Turn your PDF publications into a flip-book with our unique Google optimized e-Paper software.
7.2. GENERIC PROGRAMMING 203<br />
7.2 Generic Programming<br />
Generic programming may be viewed as having been developed in order to further facilitate<br />
the goals of code reusability and extensibility. From a general view the generic programming<br />
paradigm is about generalizing software components so that they can be directly reused easily<br />
in a wide variety of situations. While these are among the goals, which lead to the development<br />
of object oriented programming, it may vary quite profoundly in the realization. A major<br />
distinction from object oriented programming, which is focused on data structures and their<br />
states, is that it especially allows <strong>for</strong> a very abstract and orthogonal description of algorithms.<br />
To achieve this kind of generalization a separation of the basic tools of programming are important:<br />
algorithms, containers (data structures), and a glue between them (so called iterators<br />
or more generally traversors). As introduced as an important part <strong>for</strong> effective programming,<br />
the minimization of glue code, iterators and traversal objects operate as a minimal but fully<br />
abstract interface between data structures and algorithms.<br />
While the desired functionality is often implemented using static polymorphism mechanisms,<br />
such as templates in <strong>C++</strong>, generic programming should not be equated with simply programming<br />
with templates. However, when generic programming is realized using purely compile<br />
time facilities such as static polymorphism, not only is implementation ef<strong>for</strong>t reduced but the<br />
resulting run time per<strong>for</strong>mance optimized.<br />
In the following, the process of generic programming is given by elevating a procedural code to<br />
a generic one simultanioulsy fullfilling the important topics of effective programming (efficiency,<br />
type-safety, code reuse):<br />
• Algorithm: Generic algorithms are generic in two ways. First the data type which they<br />
are operating on is arbitrary and second, the type of container within which the elements<br />
are held is arbitrary.<br />
To get in touch with the generic approach, a generalization of the memcpy() function of the<br />
C standard library is discussed. An implementation of memcpy() might look somewhat<br />
like the following:<br />
void∗ memcpy(void∗ region1, const void∗ region2, size t n)<br />
{<br />
const char∗ first = (const char∗)region2;<br />
const char∗ last = ((const char∗)region2) + n;<br />
char∗ result = (char∗)region1;<br />
while (first != last)<br />
∗result++ = ∗first++;<br />
return result;<br />
}<br />
The memcpy() function is already generalized to some extent by the use of void∗ so that<br />
the function can be used to copy arrays of different kinds of data.<br />
Looking at the body of memcpy(), the function’s minimal requirements are that it needs to<br />
traverse the sequence using some sort of pointer, access the elements pointed to, copy the<br />
elements to the destination, and compare pointers to know when to stop. The memcpy()<br />
function can then be written in a generic manner:<br />
template <br />
OutputIterator