Unofficial wish list status.(Jul 2008)

Steven Schveighoffer schveiguy at yahoo.com
Wed Jul 23 11:51:25 PDT 2008


"Simen Kjaeraas" wrote
> Steven Schveighoffer wrote:
>
>> In fact, a pure member function would have tobe marked as:
>>  pure invariant void f();
>
> While I agree a pure member function would also need to be invariant,
> it seems superfluous to have to write that in the function signature.
> It /has/ to be, so why should I need to specify it?

I was showing that invariant and pure affect separate pieces of the 
function, and so I showed the syntax this way to make a point.  Most likely, 
a pure member function implies that 'this' is invariant, as there is no 
reason to have it otherwise.

>
>> As for how useful would an invariant (but not pure) function be? 
>> Consider
>> something like:
>>
>> class C
>> {
>>    int x;
>>    invariant void printTo(stream s) { s.print(x); }
>> }
>
> Could this not just as easily have been done with a const
> function, as invariant is implicitly castable to const?

Yes, and as Koroskin says, there is a benefit to having invariant vs. const 
in that you do not need to synchronize.  How important this will be in 
practice, I don't know.  But for those crazies trying to squeeze every last 
ounce of performance from an app, it will be a possible optimization point 
:)

I'll probably never use invariant in anything but pure functions (if I ever 
use those).  However, the point is, in order to be consistent with other 
functions, which can have parameters declared as invariant, you must be able 
to declare the 'this' pointer as invariant.  To not do this leaves a hole 
the same size as if you couldn't have tail-const class references.  Oh 
wait...

-Steve 





More information about the Digitalmars-d mailing list