On 9/11/07, <b class="gmail_sendername">Janice Caron</b> <<a href="mailto:caron800@googlemail.com">caron800@googlemail.com</a>> wrote:<div><span class="gmail_quote"></span><blockquote class="gmail_quote" style="border-left: 1px solid rgb(204, 204, 204); margin: 0pt 0pt 0pt 0.8ex; padding-left: 1ex;">
Hi,<br><br>the admittedly now out-of-date documentation for const says, of class<br>member functions, that the syntax for member functions which do not<br>modify member variables should be<br><br>invariant void foo()<br><br>
Does that mean that all "getter" property functions will have to be<br>re-prototyped?<br><br>As in<br><br>class C<br>{<br> private int my_x;<br> invariant int x() { return x; }<br>}<br><br>?<br></blockquote>
</div><br><br><br>So, if I've understood this correctly, the syntax for a member function which (a) does not modify any member variables, and (b) returns an invariant pointer, would be<br><br>class C<br>{<br> invariant invariant int * f() { /* body */ }
<br>}<br><br>The first "invariant" tells you (a); the second "invariant" tells you (b). This leads me to conlude that<br><br>class C<br>
{<br>
invariant int * f() { /* body */ }<br>
}<br>
<br>
is surely ambiguous. It could be interpreted either as "f is a member function of C which does not modify any of C's member variables, and which returns a pointer to int", or "f is a member function of C which is permitted to modify C's member variables, and which returns an invariant pointer to int".
<br><br>It is precisely in order to avoid this ambiguity that C++ syntax has the "const" at the end, as in<br><br>/* C++ */<br>class C<br>{<br> int * f() const { /* body */ }<br>
const int * f() { /* body */ }<br>
const int * f() { /* body */ } const<br>
}<br><br>In the above C++ example, the first function declares a member function of C which does not modify any of C's member variables, and which returns a pointer to int, while the second function returns a member function of C which is permitted to modify C's member variables, and which returns a const pointer to int. And for completeness, the third one declares a member function of C which does not modify any of C's member variables, and returns a const pointer to int.
<br><br>I will freely admit, I don't like C++'s syntax either. The whole "const at the end" business is confusing. (It's not so confusion now that I've got used to it, but it was confusing at first). I don't know whether or not D can do better, but I certainly don't like the idea of declarations being ambigous.
<br><br>I have no suggestion for how to fix this, however I bring to your attention the fact that it might need fixing.<br><br>