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 145<br />

5.2.3 More Useful Meta-functions<br />

The Boost Type Traits library [?] provides a large spectrum of meta-functions to test or manipulate<br />

attributes of types. Some of them are rather easy to implement — like the previously<br />

introduced is const — and others — like has trivial constructor or is base — require deep insight<br />

into C ++ subtleties and often into compiler internals as well. Unless one only uses very simple<br />

type traits and wants absolutely avoid the dependency of an external library, it is advisable to<br />

favor the extensively tested implementations from the type traits library over rewriting it.<br />

With the boost::is xyz we can implement special behavior <strong>for</strong> certain sets of types. One can easily<br />

add tests <strong>for</strong> domain specific type sets:<br />

template <br />

struct is matrix<br />

: boost::mpl::false<br />

{};<br />

template <br />

struct is matrix<br />

: boost::mpl::true<br />

{};<br />

// more matrix classes ...<br />

template <br />

struct is matrix<br />

: is matrix<br />

{};<br />

// more views ...<br />

Our program snippet is in line with the implementations in Boost. Instead of defining a static<br />

constant as in Section 5.2.2 we derive the meta function from boost::mpl::false and boost::mpl::true<br />

where static constants are defined with some additional typedefs. This not only shorter but<br />

requires also a bit less compile time, see [?]. 10<br />

The code is quite self-explanatory. Type we do not know are considered not being a matrix.<br />

Then we specialize <strong>for</strong> known matrix classes. For views we can further refer to the matrix-ness<br />

of the template argument.<br />

Alternatively, we can say in the type trait that every transposed view is a matrix and instead<br />

require <strong>for</strong> template arguments of transposed view that they are matrices.<br />

#include <br />

template <br />

class transposed view<br />

{<br />

BOOST STATIC ASSERT((is matrix::value)); // Make sure that the argument is a matrix type<br />

// ...<br />

};<br />

This additional assertion guarantees that the view class can only be instantiated with known<br />

matrix types. For other argument types the compilation, will terminate in this line. Un<strong>for</strong>tunately,<br />

the error message is not very in<strong>for</strong>mative <strong>for</strong> not saying confusing:<br />

10 TODO: page

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

Saved successfully!

Ooh no, something went wrong!