Logical const

Peter Alexander peter.alexander.au at gmail.com
Sun Nov 28 16:07:38 PST 2010


On 28/11/10 9:35 PM, Walter Bright wrote:
> You can write memoized functions in D, you just can't label them as
> const. You'll be relying on convention. Memoized (logically const)
> functions are not verifiable in C++, either, so you are not technically
> worse off.

I am technically worse off, just not from the compilers point of view.

I see your point about C++ const being a convention, but that doesn't 
change the fact that it is still very useful, even if it useless for the 
compiler. If I give some const object to a function:

void render(const GameObject&);

GameObject obj;
render(obj);

I can be sure that my object will come back unmodified. That it is the 
primary purpose of const. Like you said, it allows you to reason about 
your programs.

Yes, GameObject could be unreasonably mutilated by careless use of 
mutable, but in practice that simply doesn't happen.


More information about the Digitalmars-d mailing list