Safer casts

Bruce Adams tortoise_74 at yeah.who.co.uk
Fri May 9 16:50:37 PDT 2008


On Fri, 09 May 2008 19:58:49 +0100, Janice Caron <caron800 at googlemail.com>  
wrote:

> On 09/05/2008, Robert Fraser <fraserofthenight at gmail.com> wrote:
>> I'm sorry if this post sounds argumentative, but I disagree strongly  
>> with
>> pretty much everything in this post. So no hard feelings, peace,  
>> respect,
>> all that ;-P.
>
> All is peaceful here on the newsgroup. This is just a nice, friendly
> chat. Nothing has been suggested "officially", and nothing will be, if
> folk here don't like the idea.
>
>
>> >    to!(T)(x)
>> >
>> > where to! is a template function provided by std.conv (not a keyword).
>> > to! already performs many conversions. Throwing in the basics like
>> > to!(int)(double) would be an obvious extension.
>>
>>  Ick! That means another identifier that needs to be a compiler built-in
>
> Um. No. std.conv.to!(T) already exists.
>
> I guess what you're saying is, it's not possible to write a function
> like toInt(double) if the cast operator is gone. I hadn't thought of
> that. In that case, maybe it should be
>
>     cast!(T) // lossy conversion
>     to!(T) // conversion that always succeeds, or throws an exception
>
>
>> And why "union", that seems like an arbitrary keyword.
>
> It wasn't entirely arbitrary. A union /is/ a reinterpretation of bits.
> It's one memory layout painted over another. (And it's a lot shorter
> than "reinterpret_cast").
>
>
>> >    auto!(T)(x)
>>
>>  How is that better than "cast(T) x;". I guess just because it creates  
>> the
>> distinction?
>
> I guess I just couldn't think of anything better. :-) D doesn't have a
> word for "mutable", and C++'s "const_cast" is too long. (And also
> misleading, since its primary use is to /remove/ constancy, not add
> it).
>
>
>>  Okay, sorry that was so negative. But it just seems that cast() works
>> perfectly fine for _all_ those things right now. If what you want is  
>> safer
>> casting, you can write your own templates to do that.
>
> You can? I confess, my template writing skills aren't up to the job.
> If it could be done, that would be interesting. Obviously, you'd have
> to use non-reserved words for the names though.
>
> I'm curious to know if C++'s dynamic_cast<T>, const_cast<T> are
> implemented with templates. They certainly /look/ like C++ templates.

They look like templates deliberately because they do a similar job
but are not (in any implementation I have seen). The same could be made
true for any "cast!" magic. I think this is one place where breaking
the rules is more acceptable. That said, I think the reason we still have  
C style
casts in C++ is because they are the only way to implement cast templates.  
Probably
in the mysts of time this was how it was done. Does anyone have a copy of
"the design and evolution of C++" to hand? I bet the answer is in there.

On another note I like that reinterpret_cast and const_cast are hard to  
type.
It slows you down and hopefully encourages you to think about why you might
be doing something evil.
Another poster's comment that reinterpret_casts are mostly to and from  
void* hit home
for me. I don't think this is as much of a problem when your hierarchy is  
based on
object though. I also don't like your choice of union. I can see the C  
connection but it
doesn't say re-interpret to me. Something more like good=force!(evil)  
appeals.
When reviewing and correcting C++ code I make use of the _cast anchor as  
something
(a code smell) to search for. This would be harder for single words. My  
language design
axiom here would be longer names for more dangerous uses.
Otherwise I like the proposal. I hope the discussion leads to something  
more concrete.

Regards,

Bruce.





More information about the Digitalmars-d mailing list