www.digitalmars.com         C & C++   DMDScript  

digitalmars.D.learn - isRvalue trait

reply =?UTF-8?B?Tm9yZGzDtnc=?= <per.nordlow gmail.com> writes:
At

https://github.com/nordlow/phobos-next/blob/master/src/moval.d

I've implemented a helper function for creating r-value out of 
l-values defined as

E movedToRvalue(E)(ref E e)
{
     import std.algorithm.mutation : move;
     E value;
     move(e, value);             // this can be optimized
     return value;
}

For the case when movedToRvalue is called with an r-value, such 
as in,

     static assert(__traits(compiles, { consume(S(14)); }));

I would like to overload to an identity op.

Is this currently possible somehow? I cannot find any trait 
`isRvalue` that fulfills

     static assert(isRvalue!(S(14)));

Is there one?
Oct 10 2016
next sibling parent reply =?UTF-8?B?Tm9yZGzDtnc=?= <per.nordlow gmail.com> writes:
On Monday, 10 October 2016 at 11:46:01 UTC, Nordlöw wrote:
 Is there one?
Found it: http://forum.dlang.org/post/n8m8bh$2vgc$1 digitalmars.com https://issues.dlang.org/show_bug.cgi?id=15634
Oct 10 2016
parent =?UTF-8?B?Tm9yZGzDtnc=?= <per.nordlow gmail.com> writes:
On Monday, 10 October 2016 at 11:51:09 UTC, Nordlöw wrote:
 Found it:

 http://forum.dlang.org/post/n8m8bh$2vgc$1 digitalmars.com
 https://issues.dlang.org/show_bug.cgi?id=15634
Ok, so I added `isLvalue` and `isRvalue` to https://github.com/nordlow/phobos-next/blob/master/src/moval.d Now the next follow-up question becomes how to use it to restrict/overload my definition of `movedToRvalue()` since `isLvalue` takes an alias as argument and `movedToRvalue` takes a reference to an instance. How to solve this?
Oct 10 2016
prev sibling parent reply Marc =?UTF-8?B?U2Now7x0eg==?= <schuetzm gmx.net> writes:
On Monday, 10 October 2016 at 11:46:01 UTC, Nordlöw wrote:
 At

 https://github.com/nordlow/phobos-next/blob/master/src/moval.d

 I've implemented a helper function for creating r-value out of 
 l-values defined as

 E movedToRvalue(E)(ref E e)
 {
     import std.algorithm.mutation : move;
     E value;
     move(e, value);             // this can be optimized
     return value;
 }
Doesn't the second overload of `move()` already do what you want? https://dlang.org/phobos/std_algorithm_mutation.html#.move Note that in your implementation, you needlessly initialize `value`, which then needs to be properly destroyed by `move()`, which the Phobos implementation avoids: https://github.com/dlang/phobos/blob/master/std/algorithm/mutation.d#L1083-L1088
 For the case when movedToRvalue is called with an r-value, such 
 as in,

     static assert(__traits(compiles, { consume(S(14)); }));

 I would like to overload to an identity op.
If the compiler is smart enough to understand what `moveEmplace()` does, it could already do this automatically.
Oct 10 2016
parent =?UTF-8?B?Tm9yZGzDtnc=?= <per.nordlow gmail.com> writes:
On Monday, 10 October 2016 at 12:22:54 UTC, Marc Schütz wrote:
 I would like to overload to an identity op.
If the compiler is smart enough to understand what `moveEmplace()` does, it could already do this automatically.
Doh! My mistake. I'll use `moveEmplace`, then. Thx!
Oct 10 2016