[Issue 8845] Can't pass immediate or rvalue args to ref function parameters

d-bugmail at puremagic.com d-bugmail at puremagic.com
Wed Nov 27 07:18:44 PST 2013


https://d.puremagic.com/issues/show_bug.cgi?id=8845



--- Comment #17 from Manu <turkeyman at gmail.com> 2013-11-27 07:18:38 PST ---
(In reply to comment #16)
> Addressing Manu problem in first post: you cannot do it directly, but can
> workaround by 
> 
> void foo(T)(ref const T i) {}
> 
> ref lvalueOf(T)(T expression)
> {
>    return [expression][0];
> }
> 
> struct S(T)
> {
>    T t;
>    ref get() { return t; }
>    //alias get this;
> }
> 
> void main()
> {
>    //foo(0-0);
>    foo((0-0).lvalueOf);
>    foo(S!int(0-0).get);
> }
> 
> Second one is ugly but does not consume heap memory as first one, but it also
> requires that foo() does not save reference somewhere (operation is safe
> because S!int(0-0) is allocated within main scope, so passing pointer is fine,
> until it is saved).

One allocates, the other is just a subversion of the type system. It's
basically the same (and less clear?) than just doing:
  int temp = 0-0;
  foo(temp);

Either way, it's equally unsafe, and the discussion regarding rvalue->ref
parameters was mainly around safety concerns.

I was in the camp arguing for a 'scope ref' concept, which would rely on escape
analysis to enforce that a 'scope ref' doesn't escape, but we were never going
to win that argument.

The plan that Walter was most enthusiastic about was, if the function is passed
an rvalue, generate an implicit temp in the caller's stack and pass it through.
If the function being called returns a ref, it would add an implicit function
exit condition which would validate that the ref being returned is not within
the function's stack (controlled by -noboundscheck for optimisation).
This way, it would be safe to return a ref that was passed in to a function,
but not a function local. This would cascade outwards.

Frankly, I just want a solution. I like the 'scope ref' concept, but it depends
on escape analysis working well, and I'm not that precious about
implementation, I just want something that works. It's a major inhibitor to
maths intensive code, like image/geometry/physics/audio processing.
Particularly where fairly 'primitive' objects like vectors, matrices,
quaternions, colours, etc are concerned.

-- 
Configure issuemail: https://d.puremagic.com/issues/userprefs.cgi?tab=email
------- You are receiving this mail because: -------


More information about the Digitalmars-d-bugs mailing list