Structs by ref in D2
Steven Schveighoffer
schveiguy at yahoo.com
Fri Aug 21 08:10:07 PDT 2009
On Fri, 21 Aug 2009 10:42:08 -0400, Daniel Keep
<daniel.keep.lists at gmail.com> wrote:
>
>
> Lutger wrote:
>> By const ref seems reasonable to allow, but by ref is bug prone. Why is
>> it
>> handy?
>
> struct Vector4
> {
> float[4] xyzw;
> Vector opAdd(ref Vector rhs);
> }
>
> Vector code can be made much faster using ref arguments, but if you use
> ref arguments, you can't have anything more complex than a single binary
> expression.
Hence why const ref is reasonable:
Vector opAdd(ref const Vector rhs) const;
The issue is possibly with ref. There are two reasons to use ref, 1 is to
be able to change the data referenced, 2 is for passing speed.
1 is bad for rvalues. 2 should be ok for rvalues.
If there was a way to signify you only want to use ref for reason 2, there
would be a good solution. Most of the time, that's const ref, but what
you really want is head-const ref, which isn't currently possible. If a
struct has references to other values, they might be lvalues, and you may
want to be able to change them.
I'm uncertain as to how often you would want to pass an rvalue as a ref
that had lvalue reference in it, but at the very least, ref const should
be valid.
-Steve
More information about the Digitalmars-d-learn
mailing list