Thread overview
[Issue 8545] New: defined opCast disables cast(void*)this in classes
Aug 13, 2012
Andrej Mitrovic
Aug 13, 2012
Andrej Mitrovic
Aug 13, 2012
Walter Bright
Aug 13, 2012
Andrej Mitrovic
Aug 14, 2012
Andrej Mitrovic
August 13, 2012
http://d.puremagic.com/issues/show_bug.cgi?id=8545

           Summary: defined opCast disables cast(void*)this in classes
           Product: D
           Version: D2
          Platform: All
        OS/Version: All
            Status: NEW
          Severity: major
          Priority: P2
         Component: DMD
        AssignedTo: nobody@puremagic.com
        ReportedBy: andrej.mitrovich@gmail.com


--- Comment #0 from Andrej Mitrovic <andrej.mitrovich@gmail.com> 2012-08-12 20:33:46 PDT ---
class Foo
{
    T opCast(T : int)() { return 1; }
    void* test() { return cast(void*)this; }
}

void main() { }

This is problematic, cast(void*)this is the trick used to get the address of 'this' object. &this is the address of the reference and can't be used for the same purpose. So defining opCast for any type ends up disabling 'cast(void*)this'.

I honestly think we should have a druntime function or some kind of compiler intrinsic to get the equivalent of 'cast(void*)this'. That cast itself looks too hacky to begin with.

-- 
Configure issuemail: http://d.puremagic.com/issues/userprefs.cgi?tab=email
------- You are receiving this mail because: -------
August 13, 2012
http://d.puremagic.com/issues/show_bug.cgi?id=8545



--- Comment #1 from Andrej Mitrovic <andrej.mitrovich@gmail.com> 2012-08-12 20:35:06 PDT ---
Oh and the error message, not that it matters:

Error: template instance opCast!(void*) opCast!(void*) does not match template
declaration opCast(T : int)

-- 
Configure issuemail: http://d.puremagic.com/issues/userprefs.cgi?tab=email
------- You are receiving this mail because: -------
August 13, 2012
http://d.puremagic.com/issues/show_bug.cgi?id=8545


Walter Bright <bugzilla@digitalmars.com> changed:

           What    |Removed                     |Added
----------------------------------------------------------------------------
             Status|NEW                         |RESOLVED
                 CC|                            |bugzilla@digitalmars.com
         Resolution|                            |INVALID


--- Comment #2 from Walter Bright <bugzilla@digitalmars.com> 2012-08-12 21:50:50 PDT ---
I don't think this is major, because you can use a union:

{
    static union U { Object o; void* p; }
    U u;
    u.o = this;
    return u.p;
}

In fact, because a union can be used for reinterpret casting, I don't think any new syntax or semantics are needed.

-- 
Configure issuemail: http://d.puremagic.com/issues/userprefs.cgi?tab=email
------- You are receiving this mail because: -------
August 13, 2012
http://d.puremagic.com/issues/show_bug.cgi?id=8545



--- Comment #3 from Andrej Mitrovic <andrej.mitrovich@gmail.com> 2012-08-13 07:57:58 PDT ---
(In reply to comment #2)
> I don't think this is major, because you can use a union:
> 
> {
>     static union U { Object o; void* p; }
>     U u;
>     u.o = this;
>     return u.p;
> }
> 
> In fact, because a union can be used for reinterpret casting, I don't think any new syntax or semantics are needed.

I'm speaking in terms of wrapping C++ libraries (A C++ wrapper class has an inner d_object pointer which it uses to invoke D virtual methods). You're asking me to introduce overhead for every time a new class object is instantiated.

-- 
Configure issuemail: http://d.puremagic.com/issues/userprefs.cgi?tab=email
------- You are receiving this mail because: -------
August 14, 2012
http://d.puremagic.com/issues/show_bug.cgi?id=8545


art.08.09@gmail.com changed:

           What    |Removed                     |Added
----------------------------------------------------------------------------
                 CC|                            |art.08.09@gmail.com


--- Comment #4 from art.08.09@gmail.com 2012-08-14 03:21:53 PDT ---
(In reply to comment #3)
> (In reply to comment #2)
> > I don't think this is major, because you can use a union:
> > 
> > {
> >     static union U { Object o; void* p; }
> >     U u;
> >     u.o = this;
> >     return u.p;
> > }
> > 
> > In fact, because a union can be used for reinterpret casting, I don't think any new syntax or semantics are needed.
> 
> I'm speaking in terms of wrapping C++ libraries (A C++ wrapper class has an inner d_object pointer which it uses to invoke D virtual methods). You're asking me to introduce overhead for every time a new class object is instantiated.

The compiler /should/ handle it w/o any additional runtime overhead. You could also use

    void* test() { return *cast(void**)&this; }

Which approach will be more efficient probably depends on compiler implementation; ideally both versions should be free.

-- 
Configure issuemail: http://d.puremagic.com/issues/userprefs.cgi?tab=email
------- You are receiving this mail because: -------
August 14, 2012
http://d.puremagic.com/issues/show_bug.cgi?id=8545



--- Comment #5 from Andrej Mitrovic <andrej.mitrovich@gmail.com> 2012-08-14 08:18:35 PDT ---
> You could also use
> 
>     void* test() { return *cast(void**)&this; }
> 

Thanks! It does seem to create the exact same assembly even with no optimizations.

-- 
Configure issuemail: http://d.puremagic.com/issues/userprefs.cgi?tab=email
------- You are receiving this mail because: -------