On 02/22/14 18:22, andrea9940 wrote:
> I was trying to get my vector struct to use extensively references for
> passing parameters and I found a subtle bug which make me lose a few hour.
>
> A sample code that shows the bug is here http://pastebin.com/rvcNdjAE (fails
> with dmd 2.064 on linux)
> I think that the code is wrong and dmd does not recognize it:
> opBinary() allocates a struct on the stack which is then accepted by
> reference in opOpAssign.
That is ok; the problem is that opBinary returns a local stack-allocated
object by reference. Make it return by value, and with some help from RVO
you should get only one copy (which is necessary because you create a new
Vector instance).
IOW:
Vector opBinary(string op)(T rhs) const;
ref Vector opOpAssign(string op)(T rhs);
ref Vector opOpAssign(string op)(auto ref const Vector rhs);
artur