Passing structs to functions

phant0m via Digitalmars-d-learn digitalmars-d-learn at puremagic.com
Sat Jul 2 11:37:06 PDT 2016


I came from a C++ world, so I'm used to passing structs by a 
const reference (I mean the case, where a function argument isn't 
changed by the function). C++ allows passing a temporary (rvalue) 
to a function, which accepts a const reference. D doesn't allow 
this. All I have found is a message from Andrei: 
http://digitalmars.com/d/archives/digitalmars/D/const_ref_rvalues_103509.html#N103514 Sadly, he didn't describe the details there. Let's suppose he's right.
How should I pass a struct variable in D effectively? When 
passing by a const reference, I need to implement 2^N additional 
overloads, where N is the number of arguments. On the other hand, 
passing by value can be very ineffective.
Maybe I shouldn't bother at all and a compiler is smart enough to 
always optimize variables passed by value?
Please, share your best practices. There are many options here 
(immutable, const, ref, in, out, inout) and I don't know what I 
should prefer.


More information about the Digitalmars-d-learn mailing list