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.

142 CHAPTER 5. META-PROGRAMMING<br />

high-quality interface to BLAS, LAPACK and other libraries with similarly old-fashioned 6 interfaces.<br />

Conversely, as long as we only use our own functions and classes we can avoid every<br />

const cast. 7<br />

We could implement a second view class <strong>for</strong> constant matrices and overload the trans function<br />

to return this view:<br />

template <br />

class const transposed view<br />

{<br />

public:<br />

typedef typename mtl::Collection::value type value type;<br />

typedef typename mtl::Collection::size type size type;<br />

};<br />

const transposed view(const Matrix& A) : ref(A) {}<br />

const value type& operator()(size type r, size type c) const { return ref(c, r); }<br />

//private:<br />

const Matrix& ref;<br />

template <br />

const transposed view inline trans(const Matrix& A)<br />

{<br />

return const transposed view(A);<br />

}<br />

This works fine and the user could use the trans function <strong>for</strong> both constant and mutable matrices.<br />

However, a complete new class definition is a fair amount of work where just one piece of the<br />

class definition needs to be altered. For this purpose we introduce two meta-functions.<br />

Check <strong>for</strong> Constancy<br />

Our problem with the view in Listing 5.1 is that it cannot handle constant types as template<br />

argument. To modify the behavior <strong>for</strong> constant arguments we first need to find out whether<br />

an argument is constant. The meta-function that provides this in<strong>for</strong>mantion is very simple to<br />

implement by partial template specialization:<br />

template <br />

struct is const<br />

{<br />

static const bool value= false;<br />

};<br />

template <br />

struct is const<br />

{<br />

static const bool value= true;<br />

};<br />

6 To phrase it diplomatically.<br />

7 We disagree with Sutter and Alexandrescu on the other exception <strong>for</strong> using const cast [SA05, page 179],<br />

this can be handled easily with an extra function. 8

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

Saved successfully!

Ooh no, something went wrong!