Const ref and rvalues again...
martin
kinke at libero.it
Tue Nov 6 20:05:26 PST 2012
On Wednesday, 7 November 2012 at 03:35:19 UTC, Era Scarecrow
wrote:
> Still that zlib entry is coming to mind. But more importantly
> is that you still need code duplication to get both accessible.
>
> //many possible combinations thereof
> int func(const ref x);
>
> int func(int x) {
> return func(cast(const int) x);
> }
int func(in ref int x);
int func(int x) { return func(x); }
The latter overload is for rvalues (no need to cast to const) and
wouldn't be required if the first one took rvalues directly
(having a _const_ ref parameter). That's not an example for
mutable references though, it's basically a shortcut so as to not
having to declare all rvalues for x manually.
> enum ZlibEnum {}
> ZlibEnum compress(void[] output, void[] input,
> &ref Zlib state) nothrow pure;
>
> string input = "something long";
> ubyte[1000] output;
> ubyte[5] output2;
> Zlib state;
>
> compress(output, input, null);
> compress(output2, input, state);
Where's the rvalue? You're talking about an optional mutable
reference here, which is only doable via a pointer (references
cannot be null, not in C++ and not in D):
ZlibEnum compress(void[] output, void[] input, Zlib* state =
null);
...
compress(output, input);
compress(output2, input, &state);
That would be correct. But you're completely missing the point
here. Let's assume you wanted to require a state to be passed:
ZlibEnum compress(void[] output, void[] input, ref Zlib state);
You could now use:
Zlib state; // lvalue
compress(output, input, state);
but not:
compress(output, input, Zlib()); // rvalue
And that is most likely a good thing, since your state would be
lost after the compress() call. If you don't want it, you don't
pass it, and as I said, the only way to do that is to pass a
nullable pointer. So this is off-topic I'm afraid.
More information about the Digitalmars-d
mailing list