On Saturday, 22 February 2014 at 17:22:51 UTC, andrea9940 wrote:
Hi everyone,
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.

Clarifying: The bug is that opBinary returns a reference to a
local (vector), which is invalid. I don't know if dmd should see
that, as it's hidden behind a call.

I'd like to know if currently there are correct ways to pass rvalue structs by reference or if I should pass everything by value and hope the compiler optimizes all reduntant copies.

There isn't. But adding an overload that forwards to the ref
version is trivial (as long as there's only one parameter):
---
         auto opOpAssign(string op)(const Vector rhs)
         if (op == "+" || op == "-")
         {
                 return opOpAssign!op(rhs);
         }
---
I don't know if the ref even buys you anything performance-wise,
though.

Reply via email to