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.

98 CHAPTER 4. GENERIC PROGRAMMING<br />

class (on page 70) cannot be used <strong>for</strong> accumulate. What should be accumulated from a set of<br />

solvers? All the requirements <strong>for</strong> the template T of the function accumulate can be summarized<br />

as follows:<br />

• T is CopyConstructable;<br />

– Copy constructor T::T(const T&) exists so that ‘T a(b);’ is compilable if b is of type<br />

T.<br />

• T is PlusAssignable:<br />

– PlusAssign operator T::operator+=(const T&) exists so that ‘a+= b;’ is compilable if<br />

b is of type T.<br />

• T is Constructible from int<br />

– Constructor T::T(int) exists so that ‘T a(0);’ is compilable.<br />

Such a set of type requirements is called a ‘Concept’. A concept CR that contains all requirements<br />

of concept C and additional requirements is called a ‘Refinement’ of C. A type t that<br />

holds all requirements of concept C is called a ‘Model’ of C.<br />

A complete definition of a template function or type shall contain the list of required concepts<br />

like it is done <strong>for</strong> functions from the Standard Template Library, see http://www.sgi.com/<br />

tech/stl/.<br />

Today such requirements are mere documentation. A prototype of a C ++ Concept Compiler [?]<br />

that checks that<br />

• Whether a function can be called with a certain type (combination 5 );<br />

• Whether a class can be instantiated with a certain types (combination); and<br />

• Whether a function’s requirement list covers all used expressions, including those in subfunctions.<br />

The compiler generates short and comprehensive message when template functions or classes<br />

are used erroneously. People interested in generic programming shall try the compiler, it helps<br />

<strong>for</strong> better understanding. However, the compiler really is a prototype and must not be used in<br />

production code. This functionality was even planned <strong>for</strong> the next language standard but the<br />

committee could achieve a consensus on its details, to make a (very) long story short.<br />

Discussion 4.1 The most vulnerable aspect of generic programming is the semantic con<strong>for</strong>mance,<br />

that is which Semantic Concepts are modelled. For instance, an algorithm might require<br />

that a binary operation is associative to calculate correctly. One can express this requirement<br />

in the functions documentation but if someone calls this function with an operation that is not<br />

associative the compiler has no idea about this. If one violates a syntactical requirement than<br />

the compiler will complain about the missing function or operator — often in a hardly readable<br />

<strong>for</strong>m — but it will be caught no matter what. If one violates a semantic requirement the<br />

compiler generates erroneous executables and the compilation does not give not any warning<br />

because it is entirely unaware of the user types’ semantic. The only way to find such semantic<br />

errors in templates with today’s compilers is careful documentation (and its reading of course).<br />

Latest research gives hope that future C ++ standards and compilers will provide more reliable<br />

and elegant possibilities to ensure semantic correctness of template programs.<br />

5 If you have multiple template arguments

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

Saved successfully!

Ooh no, something went wrong!