[Issue 5897] New: unrelated struct type casting should ignite construction

d-bugmail at puremagic.com d-bugmail at puremagic.com
Wed Apr 27 05:31:01 PDT 2011


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

           Summary: unrelated struct type casting should ignite
                    construction
           Product: D
           Version: D2
          Platform: All
        OS/Version: All
            Status: NEW
          Severity: normal
          Priority: P2
         Component: DMD
        AssignedTo: nobody at puremagic.com
        ReportedBy: k.hara.pg at gmail.com


--- Comment #0 from Kenji Hara <k.hara.pg at gmail.com> 2011-04-27 05:27:17 PDT ---
http://www.digitalmars.com/d/2.0/expression.html
> Casting a value v to a struct S, when value is not a struct of the same type, is > equivalent to:
>
> S(v)

This spec explanation includes constructor call in D2, not only static opCall.
But current dmd implementation is not.

Following code compilation fails with -version=ctor.
----
struct A{ int n; }
struct B{
    int n, m;
  version(ctor)
  {
    this(A a){ n = a.n, m = a.n*2; }
  }
  else
  {
    static B opCall(A a)
    {
        B b;
        b.n = a.n, b.m = a.n*2;
        return b;
    }
  }
}

void main()
{
    auto a = A(10);
    auto b = cast(B)a;
    assert(b.n == 10 && b.m == 20);
}
----

-- 
Configure issuemail: http://d.puremagic.com/issues/userprefs.cgi?tab=email
------- You are receiving this mail because: -------


More information about the Digitalmars-d-bugs mailing list