On Thu, 08 Oct 2009 08:31:00 -0400, Denis Koroskin <2kor...@gmail.com> wrote:

On Thu, 08 Oct 2009 15:48:56 +0400, Steven Schveighoffer <schvei...@yahoo.com> wrote:

On Thu, 08 Oct 2009 07:26:37 -0400, Denis Koroskin <2kor...@gmail.com> wrote:

On Thu, 08 Oct 2009 14:48:19 +0400, Steven Schveighoffer <schvei...@yahoo.com> wrote:

On Wed, 07 Oct 2009 17:54:35 -0400, Denis Koroskin <2kor...@gmail.com> wrote:

On Wed, 07 Oct 2009 23:00:06 +0400, Sean Kelly <s...@invisibleduck.org> wrote:

auto x = cast(MyClass) malloc(MyClass.classinfo.init.length);

I would expect a dynamic cast to occur at this line. Which will either result in an access violation (since you are trying to cast a garbage to an object) or result in a null being returned.

malloc returns void *, so no dynamic cast.

-Steve

I know malloc returns void*. I didn't know you can hijack type system that easily.

But then, if no dynamic cast takes place why cast(Object)cast(void*)0 cannot be evaluated at compile time?

Your message made me test it :)

import std.stdio;

void *foo()
{
     return cast(void*)0;
}

void main()
{
     auto o = cast(Object)foo();
     writefln("here!");
     o.opEquals(o);
}

outputs:

here!
Segmentation fault

So, no dynamic cast (dynamic cast would have looked at the classinfo of null, segfaulting before the output).


No, IIRC, casting null to Object is perfectly valid and returns null.

Oh yeah :)  I forgot.  Should have returned 1 instead of 0.

I did also verify via obj2asm that dynamic cast was not being called :)


But you are right, casting void* to Object does a reinterpret cast instead
of dynamic cast. I'm not sure if that's a good design decision, though.

It's the only sane decision. void * is really the developer's way of saying "I'll handle the typing from here, thanks" to the compiler. So the compiler can't expect to interpret void * as anything special. There's no deterministic way to detect an object anyways, so the compiler can't make any assumptions without the typesystem.


So I would say, the fact that compile time evaluation doesn't work is a bug maybe?

-Steve

Probably. Not only it doesn't work at compile time, it doesn't work at all!

void main()
{
auto o = cast(Object)cast(void*)0; // Error: cannot cast void* to object.Object
}

Hm.. strange that my example compiles and yours does not. I'd think it to be the same thing. That definitely should be flagged as a bug.

I used dmd 2.033.

-Steve

Reply via email to