www.digitalmars.com         C & C++   DMDScript  

digitalmars.D.bugs - [Issue 1316] New: Can't directly reference members of CTFE struct arrays

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

           Summary: Can't directly reference members of CTFE struct arrays
           Product: D
           Version: 1.018
          Platform: PC
        OS/Version: Windows
            Status: NEW
          Severity: normal
          Priority: P2
         Component: DMD
        AssignedTo: bugzilla digitalmars.com
        ReportedBy: clugdbug yahoo.com.au


Two variations, with different error messages.
----------------
struct S { int a; }

int func()
{
    S [] s = [S(7)];
    return s[0].a; // Error: cannot evaluate func() at compile time
}

void main()
{    const int x = func(); }

--------------
VARIANT 2 (attempted workaround):
--------------
struct S {   int a; }

int func()
{
    S [] s = [S(7)];
    return (s[0]).a; // Fails:
// bug.d(9): Error: s is used as a type
// bug.d(9): Error: no property 'a' for type 'void[0u]'
}

void main()
{    const int x = func(); }

----------

Workaround is to index the array first, then access the member.

----------
int func()
{
    S [] s = [S(7)];
   auto q = s[0]; 
   return q.a; // OK
}


-- 
Jul 05 2007
next sibling parent d-bugmail puremagic.com writes:
http://d.puremagic.com/issues/show_bug.cgi?id=1316






The first (and most important) variant seems to have been fixed in DMD 1.019.


-- 
Jul 23 2007
prev sibling parent d-bugmail puremagic.com writes:
http://d.puremagic.com/issues/show_bug.cgi?id=1316


bugzilla digitalmars.com changed:

           What    |Removed                     |Added
----------------------------------------------------------------------------
             Status|NEW                         |RESOLVED
         Resolution|                            |WONTFIX





Variant 1 works fine now.

Regarding Variant 2: the parentheses in (s[0]) are redundant. The parser
interprets them as an attempt at a C-style cast where s[0] then must be a type.

I prefer to leave that in, as it diagnoses attempts to use C-style casts.

To fix the code, just remove the redundant parentheses.


-- 
Sep 01 2007