www.digitalmars.com         C & C++   DMDScript  

digitalmars.D.bugs - [Issue 5559] New: A static down cast in Phobos

reply d-bugmail puremagic.com writes:
http://d.puremagic.com/issues/show_bug.cgi?id=5559

           Summary: A static down cast in Phobos
           Product: D
           Version: D2
          Platform: All
        OS/Version: All
            Status: NEW
          Keywords: patch
          Severity: enhancement
          Priority: P2
         Component: Phobos
        AssignedTo: nobody puremagic.com
        ReportedBy: bearophile_hugs eml.cc



D2 lacks the static_cast of C++. It's an unsafe operation, but now and then
someone asks for it, for spots where performance is critical.

With Lars T. Kyllingstad I've written a staticDownCast(). Something similar may
be added to Phobos:


import std.stdio, std.traits, std.typetuple;

/// C++ static_cast for just down-casting
T staticDownCast(T, F)(F from) if (is(F == class) &&
                                   is(T == class) &&
                                   staticIndexOf!(F, BaseClassesTuple!T) != -1)
    in {
        assert((from is null) || cast(T)from !is null);
    } body {
        return cast(T)cast(void*)from;
    }

// some demo code

class Foo {}
class Bar : Foo {}
class Spam {}

Bar test1() {
    Foo f = new Foo;
    Bar b = cast(Bar)f;
    return b;
}

Bar test2() {
    Foo f = new Foo;
    Bar b = staticDownCast!Bar(f);
    return b;
}

void main() {
    Spam s = new Spam;
    Bar b = staticDownCast!Bar(s); // compile-time error
}


Asm produced by DMD 2.051 with -O -release -inline that shows it avoids the
dynamic cast:

_D4test5test1FZC4test3Bar   comdat
L0:     push    EAX
        mov EAX,offset FLAT:_D4test3Bar7__ClassZ
        mov ECX,offset FLAT:_D4test3Foo7__ClassZ
        push    EAX
        push    ECX
        call    near ptr __d_newclass
        add ESP,4
        push    EAX
        call    near ptr __d_dynamic_cast
        add ESP,8
        pop ECX
        ret

_D4test5test2FZC4test3Bar   comdat
L0:     push    EAX
        mov EAX,offset FLAT:_D4test3Foo7__ClassZ
        push    EAX
        call    near ptr __d_newclass
        add ESP,4
        pop ECX
        ret

-- 
Configure issuemail: http://d.puremagic.com/issues/userprefs.cgi?tab=email
------- You are receiving this mail because: -------
Feb 10 2011
next sibling parent d-bugmail puremagic.com writes:
http://d.puremagic.com/issues/show_bug.cgi?id=5559




See also:

http://www.digitalmars.com/webnews/newsgroups.php?art_group=digitalmars.D&article_id=153452

-- 
Configure issuemail: http://d.puremagic.com/issues/userprefs.cgi?tab=email
------- You are receiving this mail because: -------
Dec 26 2011
prev sibling next sibling parent d-bugmail puremagic.com writes:
http://d.puremagic.com/issues/show_bug.cgi?id=5559


Andrej Mitrovic <andrej.mitrovich gmail.com> changed:

           What    |Removed                     |Added
----------------------------------------------------------------------------
                 CC|                            |andrej.mitrovich gmail.com



08:59:41 PST ---
I think we should rather add this to the documentation than as a function into
Phobos. We don't want to encourage using this but we do want to show it's
possible to do it.

-- 
Configure issuemail: http://d.puremagic.com/issues/userprefs.cgi?tab=email
------- You are receiving this mail because: -------
Dec 18 2012
prev sibling next sibling parent d-bugmail puremagic.com writes:
http://d.puremagic.com/issues/show_bug.cgi?id=5559





 I think we should rather add this to the documentation than as a function into
 Phobos. We don't want to encourage using this but we do want to show it's
 possible to do it.
So people that want it write their own possibly buggy version, or copy&paste from the documentation their own version, with other possible copying mistakes. That's wrong. If it's an useful functionality it should be in Phobos, with unittests. If it's not useful it should be left here in bugzilla. -- Configure issuemail: http://d.puremagic.com/issues/userprefs.cgi?tab=email ------- You are receiving this mail because: -------
Dec 18 2012
prev sibling next sibling parent d-bugmail puremagic.com writes:
http://d.puremagic.com/issues/show_bug.cgi?id=5559




11:21:42 PST ---


 I think we should rather add this to the documentation than as a function into
 Phobos. We don't want to encourage using this but we do want to show it's
 possible to do it.
So people that want it write their own possibly buggy version, or copy&paste from the documentation their own version, with other possible copying mistakes.
It's unsafe by default no matter if it's in the library or if you copy-paste it from the documentation. There's no amount of unittests that will help you make static_cast bug-free and reliable. Remember that not everyone has a C++ background and they don't necessarily understand what static_cast is about, they could easily misinterpret this as just a faster version of a cast, while it's also unsafe. If people see `static_cast` in code samples (e.g. a dpaste snippet) *that's* when they'll start copy-pasting code without reading what static_cast does. Or someone on IRC will say "use static_cast because it's faster", and then people will wonder why their code segfaults. We could add a huge bold docstring reading "this is unsafe", but I'm afraid not everyone reads the documentation. Anyway try and see what Walter/Andrei think about it, they likely already have an opinion whether to include this or not. -- Configure issuemail: http://d.puremagic.com/issues/userprefs.cgi?tab=email ------- You are receiving this mail because: -------
Dec 18 2012
prev sibling parent d-bugmail puremagic.com writes:
http://d.puremagic.com/issues/show_bug.cgi?id=5559






 It's unsafe by default no matter if it's in the library or if you copy-paste it
 from the documentation.
D is a system language so despite things like safe, D programmers have the right to use unsafe features, when they want so. And "unsafety" (and user code bugs) coming from an implementation bug is not the same thing as "unsafety" (and user code bugs) coming from misuse of an unsafe feature. Also, having a common implementation of an unsafe feature, that uses one standard name, allows people to recognize it, search for the "staticDownCast" string it in codebases, and remove it if they want.
 There's no amount of unittests that will help you make
 static_cast bug-free and reliable.
I meant unittests on this library code, and not unittests of the user code.
 Remember that not everyone has a C++ background and they don't necessarily
 understand what static_cast is about, they could easily misinterpret this as
 just a faster version of a cast, while it's also unsafe.
See the answer above. D is a system language, and even in a language as Haskell you sometimes want to use unsafe operations.
 We could add a huge bold docstring reading "this is unsafe", but I'm afraid not
 everyone reads the documentation.
Then rename it "unsafeStaticDownCast", it's long and clearly unsafe. -- Configure issuemail: http://d.puremagic.com/issues/userprefs.cgi?tab=email ------- You are receiving this mail because: -------
Dec 18 2012