One more shot at this const thing.

Reiner Pope reiner.pope at gmail.com
Thu Jul 27 16:10:54 PDT 2006


Dave wrote:
> Also maybe const would apply only to built-in reference types (including 
> class objects) and not pointers, justifiable because D has these 
> built-in whereas C++ doesn't. The same 'const' would not apply to value 
> types either, for simplicity.
This presumably has the consequence that you can't get a pointer to a 
const object? Otherwise it could be trivially subverted:

   const char[] c;
   char* ptr = c.ptr;
   *ptr = 5; // Whoops! Const violation!

Pointers do seem to be one of the main sources of trouble with const, 
though, so it's an interesting idea.

> 
> The compiler can't enforce everything you can do to a const in D. To 
> make it so that const can actually be meaningful, additional language 
> could be added to the spec. That language could be as simple as 
> something like: "subverting const can result in undefined behavior".

Wouldn't that then put the onus back on the programmer to avoid const 
violations? Back to square one? Unless, of course, a const violation was 
explicitly obvious, in which case it would be an entirely different matter.

Cheers,

Reiner



More information about the Digitalmars-d mailing list