[Issue 1268] New: Struct literals try to initialize static arrays of non-static structs incorrectly

d-bugmail at puremagic.com d-bugmail at puremagic.com
Fri Jun 15 09:34:00 PDT 2007


http://d.puremagic.com/issues/show_bug.cgi?id=1268

           Summary: Struct literals try to initialize static arrays of non-
                    static structs incorrectly
           Product: D
           Version: 1.014
          Platform: PC
        OS/Version: Windows
            Status: NEW
          Keywords: diagnostic, rejects-valid
          Severity: normal
          Priority: P3
         Component: DMD
        AssignedTo: bugzilla at digitalmars.com
        ReportedBy: deewiant at gmail.com


struct S {
        int[5] x;
}

void main() {
        S s = S();
}

The above code fails to compile with the following errors:

Error: cannot implicitly convert expression (0) of type int to int[5]
Error: cannot cast int to int[5]

(Notice the lack of line numbers.)

The "S s = S();" line works at global scope, or when preceded by const or
static.

The only way to circumvent this is to add an initializer for each array
element:

struct S {
        //int[5] x = 0;              // what the compiler tries: doesn't work
        //int[5] x = [];             // void[0], not int[5]
        //int[5] x = cast(int[5])[]; // non-constant expression
        //int[5] x = [0];            // int[1], not int[5] as above

        // this works, but is tedious for long arrays
        // (although it can be automated with templates)
        int[5] x = [0,0,0,0,0];
}


-- 



More information about the Digitalmars-d-bugs mailing list