struct construction (how?)

Ali Çehreli acehreli at yahoo.com
Mon Dec 28 14:16:35 PST 2009


bearophile wrote:
 > Ali Çehreli:
 >
 >>  >> auto s = S(1, 2);
 >>
 >> Doesn't work for structs that have opCall (or maybe an opCall with
 >> matching parameters to that use).
 >
 > Try:
 > static S opCall(int x, int y) { this.x = x; this.y = y; }

As 'this' is not available in static member functions; you mean

     static S opCall(int x, int y)
     {
         S s;
         s.x = x;
         s.y = y;

         return s;
     }

But that is basically the constructor. I don't understand why opCall 
gets in the way in

   auto s = S(1, 2);

The right hand side should call the constructor. Only after an object is 
constructed, should opCall be used:

   s(3, 4);

That's what I expected, but there must be a rationale... :)

 >> I think the {} should still default initialize the
 >> remaining members (like C and C++).
 >
 > That's what it does.

Not with dmd 2.037. :(

I've responded to your message on the other thread titled "struct 
members not default initialized?". It turned out to be a bug that was 
reported about a year ago:

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

Ali


More information about the Digitalmars-d-learn mailing list