What's wrong with just a runtime-checked const?

xs0 xs0 at xs0.com
Sun Jul 16 11:22:04 PDT 2006


Reiner Pope wrote:
> 
> Wouldn't a runtime const check be much more flexible than a compile-time 
> check? Const-safeness is fundamentally a correctness-checking feature, 
> just like unit tests, so why not make it operate exactly like unit 
> tests? I'm thinking of something like array bounds checking:
> [snip]
> The even better thing about this is that most code doesn't need to have 
> const-correctness in mind when writing it, and it shouldn't break 
> existing code, because the only code that will break is code that is 
> buggy code anyway.
> 
> Am I completely missing the point?
> Will it cause memory/speed issues (keeping in mind that it's only for 
> debug builds)?

Well, I don't think you completely missed the point, but doing it would 
cause all sorts of issues:
- where should the tag be placed? you can't put it inside the pointer, 
as there are no free bits; you also can't put it next to a pointer, as 
it would affect memory layout of structures (in particular, it would 
make debug-built and release-built code non-interoperable).
- it can still be trivially subverted - just cast to int/long and back
- you can't just check at the beginning of a function - you can get the 
pointer in the middle of it; you can also get the pointer in _another_ 
function (from a global or in a multi-threaded program); checking at 
every access would be too expensive, I think, even for a debug build


xs0



More information about the Digitalmars-d-learn mailing list