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.4. META-TUNING: WRITE YOUR OWN COMPILER OPTIMIZATION 159<br />

The print-outs shall show us the execution. For convenience, one can templatize the operator<br />

on the argument types:<br />

template <br />

struct fsize assign<br />

{<br />

template <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 />

template <br />

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

{<br />

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

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

}<br />

};<br />

Then the vector types can by deduced by the compiler when the operator is called. Instead of<br />

the previous loop, we call the assignment functor in the operator:<br />

template <br />

class fsize vector<br />

{<br />

BOOST STATIC ASSERT((my size > 0));<br />

};<br />

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

{<br />

fsize assign()(∗this, that);<br />

return ∗this;<br />

}<br />

template <br />

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

{<br />

fsize assign()(∗this, that);<br />

return ∗this;<br />

}<br />

The execution of the following code fragment<br />

yields<br />

fsize vector v, w;<br />

v[0]= v[1]= 1.0; v[2]= 2.0; v[3]= −3.0;<br />

w= v;

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

Saved successfully!

Ooh no, something went wrong!