Declaring rvalue function arguments

maik klein via Digitalmars-d-learn digitalmars-d-learn at puremagic.com
Sun Jan 31 09:48:53 PST 2016


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 rvalue?
>>
>> For example, in C++ I can do this:
>> [code]
>> void foo(int && x) {...}
>>
>> foo(5); // Works fine
>> int y = 5;
>> foo(y); // Compile error; y is not an rvalue
>> [/code]
>>
>> This functionality turns out to be really useful when dealing 
>> with
>> transferring ownership of resources.
>
> 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 */
> }
> ----

The problem is that x will be copied afaik which is not what you 
want if you want to deal with ownership.


More information about the Digitalmars-d-learn mailing list