auto ref

Simen kjaeraas simen.kjaras at gmail.com
Wed Dec 16 17:16:56 PST 2009


On Wed, 16 Dec 2009 12:00:46 +0100, KennyTM~ <kennytm at gmail.com> wrote:

> On Dec 16, 09 15:18, Walter Bright wrote:
>> There's a need in generic code to have a function take a parameter by
>> ref if it is an lvalue, and by value if it is an rvalue. This can be
>> addressed by making it a template using auto ref:
>>
>> T foo(T)(auto ref T x) { ... }
>>
>> foo(3) // call by value
>> int y;
>> foo(y) // call by reference
>>
>> There is also a need to 'transmit' the ref'ness to the return value.
>> This can be done with auto ref:
>>
>> auto ref foo(T)(auto ref T x) { return x; }
>>
>> foo(3) => int foo(int x)
>> foo(y) => ref int foo(ref int x)
>>
>> This means that the generic forwarding function would look like:
>>
>> auto ref foo(alias F, T...)(auto ref T args) { return F(args); }
>
> auto const?

auto const auto ref Foo bar( auto const auto ref Foo arg ) {
     return arg;
}

Am I the only one who finds this confusing?

-- 
Simen



More information about the Digitalmars-d mailing list