www.digitalmars.com         C & C++   DMDScript  

digitalmars.D.bugs - [Issue 2427] New: Function call in struct initializer fails to compile

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

           Summary: Function call in struct initializer fails to compile
           Product: D
           Version: 2.020
          Platform: PC
        OS/Version: Windows
            Status: NEW
          Keywords: rejects-valid
          Severity: normal
          Priority: P2
         Component: DMD
        AssignedTo: bugzilla digitalmars.com
        ReportedBy: samukha voliacable.com


Compiler tries to CTFE the function:

struct S
{
    int x;
}

int foo(int i)
{
    return i;
}

void main()
{
    int i;
    S s = { foo(i) };
    return 0;
}
----
test.d(20): Error: variable i is used before initialization


-- 
Oct 23 2008
next sibling parent d-bugmail puremagic.com writes:
http://d.puremagic.com/issues/show_bug.cgi?id=2427






'return 0;' in the example is noise. please ignore it.


-- 
Oct 23 2008
prev sibling next sibling parent d-bugmail puremagic.com writes:
http://d.puremagic.com/issues/show_bug.cgi?id=2427


bugzilla digitalmars.com changed:

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





The { } initializers for structs are for statically initialized data only. To
use a dynamic initializer,

    S s = S( foo(i) );


-- 
Oct 26 2008
prev sibling parent d-bugmail puremagic.com writes:
http://d.puremagic.com/issues/show_bug.cgi?id=2427


samukha voliacable.com changed:

           What    |Removed                     |Added
----------------------------------------------------------------------------
             Status|RESOLVED                    |REOPENED
         Resolution|INVALID                     |





From the spec:
"The static initializer syntax can also be used to initialize non-static
variables, provided that the member names are not given. The initializer need
not be evaluatable at compile time.

void test(int i)
{
    S s = { 1, i };   // q.a = 1, q.b = i, q.c = 0, q.d = 7
}"


-- 
Oct 26 2008