Declaring rvalue function arguments

anonymous via Digitalmars-d-learn digitalmars-d-learn at puremagic.com
Sun Jan 31 09:42:19 PST 2016


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 */
}
----


More information about the Digitalmars-d-learn mailing list