Is this a operator overloading bug?

Bill Baxter dnewsgroup at billbaxter.com
Thu Jan 11 00:03:16 PST 2007


I have confirmed that the code below works properly in DMD 0.177 but 
fails in DMD 0.178 (in the same way it does in 1.00), so it does look 
very much like the NRVO is indeed the culprit.

Can you file a bug about it with bugzilla?
   http://d.puremagic.com/issues/

You can get around it for now by making a constructor function (using 
static opCall is popular), and writing your opMul as
    return the_constructor_func(x*s, y*s, z*s);

That seems to work, although I suspect that just means that it's 
disabling the NRVO, which I guess isn't smart enough to do the 
optimization when the return value is passed through several functions.

--bb

Tim Fang wrote:
>     I am very new to D and I am trying to implement a Vector3 struct, please 
> take a look at the code below, why the output is "nananan"? I use dmd 1.00.
> 
> --code---------------------
> import std.stdio;
> 
> void main()
> {
>  Vector3 a;
>  a.set(1,1,1);
>  a = a*2;
>  writefln(a.x, a.y, a.z);
> }
> 
> 
> struct Vector3
> {
>  float x,y,z;
> 
>  // constructor
>  void set(float _x, float _y, float _z)
>  {
>   x = _x;
>   y = _y;
>   z = _z;
>  }
> 
>  Vector3 opMul(float s)
>  {
>   Vector3 ret;
>   ret.x = x*s;
>   ret.y = y*s;
>   ret.z = z*s;
>   return ret;
>  }
> } 
> 
> 



More information about the Digitalmars-d mailing list