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.

140 CHAPTER 5. META-PROGRAMMING<br />

First of all, is the ref member really constant? We never used const in the class definition or<br />

the function trans. Help is provided from the ‘Run-Time Type Identification (RTTI)’. We add<br />

the header ‘typeinfo’ and print the type in<strong>for</strong>mation:<br />

#include <br />

...<br />

std::cout ≪ ”typeid of trans(A) = ” ≪ typeid(tst::trans(A)).name() ≪ ’\n’;<br />

std::cout ≪ ”typeid of trans(B) = ” ≪ typeid(tst::trans(B)).name() ≪ ’\n’;<br />

This will produce the following output: 4<br />

typeid of trans(A) = N3tst15transposed_viewIN3mtl6matrix7dense2DIfNS2_10<br />

parametersINS1_3tag9row_majorENS1_5index7c_indexENS1_9non_fixed10<br />

dimensionsELb0EEEEEEE<br />

typeid of trans(B) = N3tst15transposed_viewIKN3mtl6matrix7dense2DIfNS2_10<br />

parametersINS1_3tag9row_majorENS1_5index7c_indexENS1_9non_fixed10<br />

dimensionsELb0EEEEEEE<br />

The output is apparently not very clear. However, if we look very careful, we see the extra<br />

‘K’ in the second line that tells us that the view is instantiated with a constant matrix type.<br />

Another disadvantage of RTTI is that we only see the const attribute of template parameters.<br />

That is printing the type in<strong>for</strong>mantion of trans(B).ref would not tell wether or not this type is<br />

constant.<br />

An alternative that solves both problems is inspecting the type by provocing an error message.<br />

We can <strong>for</strong> instance write:<br />

int ta= trans(A);<br />

int tb= trans(B);<br />

Then the compiler gives us a message like:<br />

trans_const.cpp:120: Error: ≫mtl::matrix::transposed_view >≪ cannot be converted to ≫int≪<br />

in initialization<br />

trans_const.cpp:121: Error: ≫const mtl::matrix::transposed_view≪ cannot be<br />

converted to ≫int≪ in initialization<br />

Here the types are much more readable. 5 We can see clearly that trans(B) returns a view with<br />

a constant template parameter. The same trick could be done <strong>for</strong> the reference in the view:<br />

int tar= trans(A).ref;<br />

int tbr= trans(B).ref;<br />

The error message would be accordingly:<br />

4<br />

With g++, on other compilers it might be different but the essential in<strong>for</strong>mation will be the same. The lines<br />

are broken manually.<br />

5<br />

TODO: Why the hell is this const outside in line 121???

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

Saved successfully!

Ooh no, something went wrong!