Safer casts

Dee Girl deegirl at noreply.com
Fri May 9 22:21:32 PDT 2008


Janice Caron Wrote:

> On 10/05/2008, Dee Girl <deegirl at noreply.com> wrote:
> > With much interest I followed this thread. Forgive my ignorance, but I can not understand one thing. Could this functionality be implemented as library functions all using the built in cast?
> 
> Not really, since part of the proposal is that
> 
>    const c = new C;
>    auto d = cast(C)c; // ERROR - cast cannot remove constancy
> 
> should not compile. I see ruling this out as an important part of
> const correctness.
> 
> If you make it optional (e.g. have some sort of template,
> static_cast!(T)), then people are just going to write "cast" instead
> of "static_cast!" because (a) it's shorter, and (b) the programmer's
> inherent belief that everything they write is bug free. That's exactly
> what happens in C++.

But in C++ I can not grep for old-style cast. In D I can (but see my other message...)

Another thing I noticed:

class C {}

void main(string[] args)
{
    auto c = new const C;
}

Does not compile. But if I change to const(C) it does compile. It is a bit unusual that parens make such a big difference. Thank you, Dee Girl



More information about the Digitalmars-d mailing list