[Issue 1262] New: Local variable of struct type initialized by literal resets when compared to .init

d-bugmail at puremagic.com d-bugmail at puremagic.com
Fri Jun 8 05:03:33 PDT 2007


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

           Summary: Local variable of struct type initialized by literal
                    resets when compared to .init
           Product: D
           Version: 1.014
          Platform: PC
        OS/Version: Linux
            Status: NEW
          Keywords: wrong-code
          Severity: normal
          Priority: P2
         Component: DMD
        AssignedTo: bugzilla at digitalmars.com
        ReportedBy: fvbommel at wxs.nl


(First seen in a message posted by "HATA" to d.D.bugs,
http://www.digitalmars.com/webnews/newsgroups.php?art_group=digitalmars.D.bugs&article_id=11355)
---
import std.stdio;

struct A { int v; }

void main() {
    A a = A(10);

    writefln("Before test 1: ", a.v);
    if (a == a.init) writefln(a.v,"(a==a.init)");
    else writefln(a.v,"(a!=a.init)");

    a.v = 100;
    writefln("Before test 2: ", a.v);
    if (a == a.init) writefln(a.v,"(a==a.init)");
    else writefln(a.v,"(a!=a.init)");

    a = A(1000);
    writefln("Before test 3: ", a.v);
    if (a == a.init) writefln(a.v,"(a==a.init)");
    else writefln(a.v,"(a!=a.init)");
}
---
Even though the value of a.v is not 10 before the last two if statements, it
gets set to 10 right before the test.

This doesn't happen if 'a' is a global variable, or if a static opCall(int)
with traditional implementation is added. So this is a workaround:
---
struct A {
    int v;
    static A opCall(int x) {
        A result;
        result.v = x;
        return result;
    }
}
---


-- 



More information about the Digitalmars-d-bugs mailing list