Declaring rvalue function arguments

2016-01-31 Thread Matt Elkins via Digitalmars-d-learn
I know I can mark an argument ref to require lvalues, so I'm wondering whether there is an equivalent for rvalues; that is, is there a way to specify that an argument to a function MUST be an rvalue? For example, in C++ I can do this: [code] void foo(int && x) {...} foo(5); // Works fine int

Re: Declaring rvalue function arguments

2016-01-31 Thread maik klein via Digitalmars-d-learn
On Sunday, 31 January 2016 at 17:42:19 UTC, anonymous wrote: On 31.01.2016 18:21, Matt Elkins wrote: I know I can mark an argument ref to require lvalues, so I'm wondering whether there is an equivalent for rvalues; that is, is there a way to specify that an argument to a function MUST be an

Re: Declaring rvalue function arguments

2016-01-31 Thread Matt Elkins via Digitalmars-d-learn
Errr, ignore the makeFoo() line. Left that in by accident, has no bearing on the issue.

Re: Declaring rvalue function arguments

2016-01-31 Thread Matt Elkins via Digitalmars-d-learn
On Sunday, 31 January 2016 at 18:02:19 UTC, Matt Elkins wrote: Here is the one I am using right now: Actually, here is the whole module in case you are interested in the unittests/usage: [code] import std.algorithm; import std.traits; struct ResourceHandle(T, alias Deleter, T Default =

Re: Declaring rvalue function arguments

2016-01-31 Thread maik klein via Digitalmars-d-learn
On Sunday, 31 January 2016 at 17:21:54 UTC, Matt Elkins wrote: I know I can mark an argument ref to require lvalues, so I'm wondering whether there is an equivalent for rvalues; that is, is there a way to specify that an argument to a function MUST be an rvalue? For example, in C++ I can do

Re: Declaring rvalue function arguments

2016-01-31 Thread anonymous via Digitalmars-d-learn
On 31.01.2016 18:21, Matt Elkins wrote: I know I can mark an argument ref to require lvalues, so I'm wondering whether there is an equivalent for rvalues; that is, is there a way to specify that an argument to a function MUST be an rvalue? For example, in C++ I can do this: [code] void foo(int

Re: Declaring rvalue function arguments

2016-01-31 Thread Matt Elkins via Digitalmars-d-learn
On Sunday, 31 January 2016 at 17:42:19 UTC, anonymous wrote: I don't know if this works in all cases, but it passes that simple test: @disable void foo(ref int x); void foo(int x) {} void main() { foo(5); /* works */ int y = 5; foo(y); /* error */ } My fault, I should

Re: Declaring rvalue function arguments

2016-01-31 Thread maik klein via Digitalmars-d-learn
On Sunday, 31 January 2016 at 17:55:53 UTC, Matt Elkins wrote: Errr, ignore the makeFoo() line. Left that in by accident, has no bearing on the issue. I have found an interesting SO answer http://stackoverflow.com/a/35114945/944430 This would explain everything that we would need. I am just

Re: Declaring rvalue function arguments

2016-01-31 Thread Matt Elkins via Digitalmars-d-learn
On Sunday, 31 January 2016 at 17:48:53 UTC, maik klein wrote: The problem is that x will be copied afaik which is not what you want if you want to deal with ownership. I think that can be solved by wrapping the resource in a struct that deals with passing the ownership. Here is the one I am

Re: Declaring rvalue function arguments

2016-01-31 Thread Matt Elkins via Digitalmars-d-learn
On Sunday, 31 January 2016 at 17:55:53 UTC, Matt Elkins wrote: Errr, ignore the makeFoo() line. Left that in by accident, has no bearing on the issue. Ok, I think I understand why this doesn't work, at least. The Foo passed into bar() is, of course, an lvalue itself. So I can achieve this

Re: Declaring rvalue function arguments

2016-01-31 Thread Jonathan M Davis via Digitalmars-d-learn
On Sunday, January 31, 2016 17:48:53 maik klein via Digitalmars-d-learn wrote: > On Sunday, 31 January 2016 at 17:42:19 UTC, anonymous wrote: > > On 31.01.2016 18:21, Matt Elkins wrote: > > I don't know if this works in all cases, but it passes that > > simple test: > > > > > > @disable void