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.

5.2. PROVIDING TYPE INFORMATION 149<br />

4. The function overload has no return type;<br />

5. Is there<strong>for</strong>e ignored.<br />

For short, the overload is only enabled if the argument is a matrix — as the names of the<br />

meta-functions say. Likewise the second overload is only available <strong>for</strong> vectors. A short test<br />

demonstrates this:<br />

mtl::dense2D A(3, 3);<br />

A= 2, 3, 4,<br />

5, 6, 7,<br />

8, 9, 10;<br />

mtl::dense vector v(3);<br />

v= 3, 4, 5;<br />

std::cout ≪ ”one norm(A) is ” ≪ tst::one norm(A) ≪ ”\n”;<br />

std::cout ≪ ”one norm(v) is ” ≪ tst::one norm(v) ≪ ”\n”;<br />

For types that are neither matrix or vector it will look as there is no function one norm at all.<br />

Types that are considered both matrix and vector would cause an ambiguity.<br />

Draw-backs: The mechanism of enable if is very powerful but not particularly pleasant to<br />

debug. Error messages caused by enable if are usually rather long but not very meaningful. If<br />

a function match is missing <strong>for</strong> a given argument type, it is hard to determine why because no<br />

helpful in<strong>for</strong>mation is provided to the programmer, he/she is only told that no match is found,<br />

period. The enabling mechanism can not select the most specific condition. For instance,<br />

we cannot specialize implementation <strong>for</strong> say is sparse matrix. This can be achieved by avoid<br />

ambiguities in the conditions:<br />

template <br />

typename boost::enable if c::type<br />

inline one norm(const T& A);<br />

template <br />

typename boost::enable if::type<br />

inline one norm(const T& A);<br />

Evidently, this will become quite confusing if too many hierarchical conditions are considered.<br />

The SFINAE paradigm only applies to template arguments of the function itself. There<strong>for</strong>e,<br />

member functions cannot be enabled depending on the class’ template argument. For instance,<br />

the mutable access operator in line 9 of Listing 5.1 cannot be hidden with enable if <strong>for</strong> views on<br />

constant matrices because the operator itself is not a template function. There are possibilities<br />

to introduce a template argument artificially <strong>for</strong> a member function to enable enable if but this<br />

really does not contribute to the clarity of the program.<br />

Concepts can handle hierarchies in conditions, non-template member functions and provide also<br />

more helpful error messages. Un<strong>for</strong>tunately, they will not be available in C ++0x and it is not<br />

clear yet when they will usable <strong>for</strong> mainstream programming.

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

Saved successfully!

Ooh no, something went wrong!