cast(!const) proposal

Janice Caron caron800 at googlemail.com
Tue Sep 11 12:25:18 PDT 2007


On 9/11/07, Steven Schveighoffer <schveiguy at yahoo.com> wrote:
> I interpreted your post as you were trying to say if you have a mutable
> pointer to immutable data, you cannot cast the mutable pointer type into a
> mutable integer without doing a !const cast.

I was.


> That was where my disagreement was.

Oh. Right. Well then, I guess that /was/ a disagreement. But I've
changed my mind now and I think you're right.


> Since the pointer is mutable, it should be perfectly legal to cast it
> into another compatible mutable type without a !const cast.

You can argue either way. My take was that, without a cast(!const),
the only thing you should be allowed to convert a pointer-to-const
into is another pointer-to-const.

(You should be allowed to add constness, of course, but not to remove it).

This isn't about catching tricksters, it's about preventing
/accidents/. So if I type:

auto n = cast(intptr_t) p;

I thought it might be kinda nice if the compiler could "remind" me if
p was a pointer-to-const, so that I didn't do it by accident.

But on second thoughts, there's really no justification for insisting
on a cast(!const) here, because const-correctness isn't necessarily
being abused.

Points to you on this one then. :-)
Cheers



More information about the Digitalmars-d mailing list