www.digitalmars.com         C & C++   DMDScript  

digitalmars.D.bugs - [Issue 15685] New: &slice[$] should be allowed


          Issue ID: 15685
           Summary: &slice[$] should be allowed
           Product: D
           Version: D2
          Hardware: All
                OS: All
            Status: NEW
          Severity: enhancement
          Priority: P1
         Component: dmd
          Assignee: nobody puremagic.com
          Reporter: naptimeentertainment gmail.com

It should be possible to get the address of the upper boundary of a slice by
taking the address of slice[$].  Here is the address I want (and I hereby
release this untested code snippet into the public domain):

pragma (inline, true) inout(Elem)*
addrOfDollar(Elem) (auto ref inout(Elem)[] slice)  trusted
    import core.stdc.stdint : uintptr_t;
    immutable uintptr_t
        pHead = cast(uintptr_t) slice.ptr, // &slice[0]
        nmemb = slice.length,
        esize = Elem.sizeof,
        pDoll = nmemb * esize + pHead; // &slice[$]
    return cast(typeof(return)) pDoll;

But there are two issues with that:

1.  I want to use the syntax &slice[$] instead of calling a function.
2.  It doesn't work during CTFE.

Feb 15 2016