struct construction (how?)

Steven Schveighoffer schveiguy at yahoo.com
Mon Dec 28 13:15:44 PST 2009


On Mon, 28 Dec 2009 10:40:58 -0500, Ali Çehreli <acehreli at yahoo.com> wrote:

> One issue remains, which prompted me to open this thread in the first  
> place:
>
> I wanted to experiment with defining opCall for that struct:
>
> struct S
> {
>      int x;
>      int y;
>
>      const int opCall(int p0, int p1)
>      {
>          return p0 + p1;
>      }
> }
>
> This does not compile anymore:
>
>      auto s = S(1, 2);
>      s(3, 4);          // hoping to call opCall
>
> But compiler error instead:
>
> Error: function expected before (), not s of type int
>
> See, the type of 's' is 'int', meaning that S(1,2) is not a constructor  
> but a call to opCall. (This behavior documented on the struct spec page.)
>
> Here is a consistent deduction of that behavior:
>
> - S(1,2) is always the opCall
> - if the programmer doesn't define an opCall, the automatic one is  
> called and the automatic one initializes the members

Removing s(3, 4), you get the following error message:

testbug.d(14): Error: need 'this' to access member opCall

So it appears that the compiler isn't outputting this error message when  
it should.

-Steve


More information about the Digitalmars-d-learn mailing list