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.

158 CHAPTER 5. META-PROGRAMMING<br />

A state-of-the-art compiler will recognize that all iterations are independent one from each<br />

other, e.g., data[2]= that[2]; is independent of data[1]= that[1];. The compiler will also determine<br />

the size of loop during compilation. As a consequence, the generated binary of a type with size<br />

3 will be equivalent to:<br />

template <br />

class fsize vector<br />

{<br />

template <br />

self& operator=(const self& that)<br />

{<br />

data[0]= that[0];<br />

data[1]= that[1];<br />

data[2]= that[2];<br />

}<br />

};<br />

The right-hand-side vector that might be an expression template § 5.3 <strong>for</strong> say alpha ∗ x + y and<br />

its evaluation will be also inlined:<br />

template <br />

class fsize vector<br />

{<br />

template <br />

self& operator=(const self& that)<br />

{<br />

data[0]= alpha ∗ x[0] + y[0];<br />

data[1]= alpha ∗ x[1] + y[1];<br />

data[2]= alpha ∗ x[2] + y[2];<br />

}<br />

};<br />

To make the unrolling more explicit and <strong>for</strong> the sake of step-wise introducing meta-tuning we<br />

develop a functor that computes the assignment:<br />

template <br />

struct fsize assign<br />

{<br />

void operator()(Target& tar, const Source& src)<br />

{<br />

fsize assign()(tar, src);<br />

std::cout ≪ ”assign entry ” ≪ N ≪ ’\n’;<br />

tar[N]= src[N];<br />

}<br />

};<br />

template <br />

struct fsize assign<br />

{<br />

void operator()(Target& tar, const Source& src)<br />

{<br />

std::cout ≪ ”assign entry ” ≪ 0 ≪ ’\n’;<br />

tar[0]= src[0];<br />

}<br />

};

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

Saved successfully!

Ooh no, something went wrong!