Thread overview | |||||||
---|---|---|---|---|---|---|---|
|
January 02, 2012 rvalue references template ? | ||||
---|---|---|---|---|
| ||||
Is it possible to create a template turning any value into a lvalue? This would be helpful if a function expects a reference but you dont need the result of the change: ///decode(S)(in S str, ref size_t index); auto c = std.utf.decode(some_string, lval!0); |
January 02, 2012 Re: rvalue references template ? | ||||
---|---|---|---|---|
| ||||
Posted in reply to Joshua Reusch | On 01/02/2012 03:02 PM, Joshua Reusch wrote:
> Is it possible to create a template turning any value into a lvalue?
> This would be helpful if a function expects a reference but you dont
> need the result of the change:
>
> ///decode(S)(in S str, ref size_t index);
> auto c = std.utf.decode(some_string, lval!0);
Yes, but I currently cannot see a full solution that does not heap-allocate.
|
January 02, 2012 Re: rvalue references template ? | ||||
---|---|---|---|---|
| ||||
Posted in reply to Joshua Reusch | On Mon, 02 Jan 2012 15:02:30 +0100, Joshua Reusch <yoschi@arkandos.de> wrote:
> Is it possible to create a template turning any value into a lvalue?
> This would be helpful if a function expects a reference but you dont need the result of the change:
>
> ///decode(S)(in S str, ref size_t index);
> auto c = std.utf.decode(some_string, lval!0);
template LRef( T, string f = __FILE__, int l = __LINE__ ) {
static T LRef;
}
ref T lref( T, string f = __FILE__, int l = __LINE__ )( T value ) {
LRef!( T, f, l ) = value;
return LRef!( T, f, l );
}
unittest {
assert( __traits( compiles, lref(0) = 3 ) );
}
This seems to work for me. 'course, there are limitations. Behind the
scenes, this creates a global variable per instantiation, and it
cannot be used more than once per line.
|
January 02, 2012 Re: rvalue references template ? | ||||
---|---|---|---|---|
| ||||
Posted in reply to Joshua Reusch | On Mon, 02 Jan 2012 15:02:30 +0100, Joshua Reusch <yoschi@arkandos.de> wrote:
> Is it possible to create a template turning any value into a lvalue?
> This would be helpful if a function expects a reference but you dont need the result of the change:
>
> ///decode(S)(in S str, ref size_t index);
> auto c = std.utf.decode(some_string, lval!0);
template LRef( T, string f = __FILE__, int l = __LINE__ ) {
static T LRef;
}
ref T lref( T, string f = __FILE__, int l = __LINE__ )( T value ) {
LRef!( T, f, l ) = value;
return LRef!( T, f, l );
}
unittest {
assert( __traits( compiles, lref(0) = 3 ) );
}
This seems to work for me. 'course, there are limitations. Behind the
scenes, this creates a global variable per instantiation, and it
cannot be used more than once per line.
|
January 02, 2012 Re: rvalue references template ? | ||||
---|---|---|---|---|
| ||||
Posted in reply to Simen Kjærås | Am 02.01.2012 22:13, schrieb Simen Kjærås:
> On Mon, 02 Jan 2012 15:02:30 +0100, Joshua Reusch <yoschi@arkandos.de>
> wrote:
>
>> Is it possible to create a template turning any value into a lvalue?
>> This would be helpful if a function expects a reference but you dont
>> need the result of the change:
>>
>> ///decode(S)(in S str, ref size_t index);
>> auto c = std.utf.decode(some_string, lval!0);
>
>
> template LRef( T, string f = __FILE__, int l = __LINE__ ) {
> static T LRef;
> }
>
> ref T lref( T, string f = __FILE__, int l = __LINE__ )( T value ) {
> LRef!( T, f, l ) = value;
> return LRef!( T, f, l );
> }
>
> unittest {
> assert( __traits( compiles, lref(0) = 3 ) );
> }
>
> This seems to work for me. 'course, there are limitations. Behind the
> scenes, this creates a global variable per instantiation, and it
> cannot be used more than once per line.
I tried something similar, but always got some compiler errors... I think I tried to change a compile-time value. But your function works !
Thank you !
|
Copyright © 1999-2021 by the D Language Foundation