www.digitalmars.com         C & C++   DMDScript  

digitalmars.D.bugs - [Issue 8545] New: defined opCast disables cast(void*)this in classes

reply d-bugmail puremagic.com writes:
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



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: -------
Aug 12 2012
next sibling parent d-bugmail puremagic.com writes:
http://d.puremagic.com/issues/show_bug.cgi?id=8545




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: -------
Aug 12 2012
prev sibling next sibling parent d-bugmail puremagic.com writes:
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



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: -------
Aug 12 2012
prev sibling next sibling parent d-bugmail puremagic.com writes:
http://d.puremagic.com/issues/show_bug.cgi?id=8545




07:57:58 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.
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: -------
Aug 13 2012
prev sibling next sibling parent d-bugmail puremagic.com writes:
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





 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: -------
Aug 14 2012
prev sibling parent d-bugmail puremagic.com writes:
http://d.puremagic.com/issues/show_bug.cgi?id=8545




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: -------
Aug 14 2012