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





------- Comment #1 from samukha voliacable.com  2008-10-23 09:45 -------
'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




------- Comment #2 from bugzilla digitalmars.com  2008-10-26 04:41 -------
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                     |




------- Comment #3 from samukha voliacable.com  2008-10-26 05:14 -------
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