non-const method permitted on rvalue?

Ali Çehreli via Digitalmars-d-learn digitalmars-d-learn at puremagic.com
Fri Oct 24 08:11:46 PDT 2014


On 10/24/2014 07:49 AM, Shriramana Sharma via Digitalmars-d-learn wrote:

 > the compilers
 > are complaining only when I try to assign directly to the member of an
 > rvalue, but if I try to assign via a non-const ref returned by a
 > non-const method, then it's apparently fine?!! At least shouldn't D
 > prohibit this? [Yet another case of rvalue refs being allowed to
 > escape?]
 >
 > nonconst.d:
 > --
 > struct Pair {
 >      int x, y ;
 >      ref Pair handle() { return this ; }
 > }
 > void main () {
 >      Pair(1, 2).x = 5 ;

Just to be clear, the above does not compile and the following does compile.

 >      Pair(1, 2).handle.x = 5 ;
 > }

Without flow analysis, it is impossible for the compiler to see that 
what handle() returns is an rvalue. Assuming that 'that' is an lvalue, 
the following should not be disallowed:

     ref Pair handle() {
         return isSomeCondition() ? this : that ;
     }

Ali



More information about the Digitalmars-d-learn mailing list