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.

3.7. ACCESSING OBJECT MEMBERS 87<br />

comma operator has to be applied on them. A language that would allow such dependent<br />

conversions would end up in extremely long compile times to considered all possibilities 12 and<br />

probability of ambiguities would increase tremendously.<br />

Instead the compiler considers ‘0, 0’ as a sequence of two expressions where each expression is<br />

an integer constant. The result of a sequence is the result of the last expression, i.e. the integer<br />

constant zero in our case. This cannot be converted into a double index.<br />

To throw in a really bad idea, we give the second constructor argument of double index a default<br />

value:<br />

struct double index<br />

{<br />

double index (int i1, int i2= 0) // Very bad<br />

: i1 (i1), i2 (i2) {}<br />

int i1, i2;<br />

};<br />

Then the expression A[0, 1] compiles, as well as A[0, 1, 2, 3, 4]. It evaluates the integer sequence<br />

and the result is the last expression. A single integer can be implicitly converted into double index.<br />

As a result, the last integer is considered the row and the column is zero.<br />

Comparing the approaches<br />

The previous implementations show that C ++ allows us to provide different notations <strong>for</strong> userdefined<br />

types and we can implement it in the manner that seems most appropriate to us. The<br />

first approach was replacing square brackets by round parentheses to enable multiple arguments.<br />

This was the simplest solution and if one is willing to accept this syntax, one can safe oneself<br />

the length we went through to come up with a fancier notation. The technique of returning a<br />

pointer was not complicated either but it relies to strongly on the internal representation. If<br />

we use some internal blocking or some other specialized internal storage scheme, we will need<br />

an entirely different technique. Another drawback was that we cannot test the range of the<br />

column index.<br />

The last approach, introduced special types and the fact that we must always specify the type<br />

of the index explicitly makes the notation <strong>for</strong> constant indices clumsier instead of clearer. It<br />

also introduced a lot of implicit conversions and in a large code base we might have enormous<br />

trouble to avoid ambiguities. Another un<strong>for</strong>tunate aspect is the overloading of the comma<br />

operator. It makes the understanding of programs more difficult — because one has to pay a<br />

lot of attention to the types of expressions to distinguish it from non-overloaded sequences —<br />

and can cause weird affects. Thus, our first recommendation is keep reading since the proxy<br />

solution in § ?? is in our opinion preferable to the previous approaches (although not perfect<br />

either).<br />

Resuming, C ++ gives us the opportunity to handle programming tasks in different ways. Several<br />

times, none of the solutions will be perfect. Even if oneself is satisfied with the solution,<br />

then there will be most certainly some (allegedly) experienced C ++ programmer who finds a<br />

disadvantage.<br />

12 It might even become undecidable.

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

Saved successfully!

Ooh no, something went wrong!