non-const method permitted on rvalue?

Shriramana Sharma via Digitalmars-d-learn digitalmars-d-learn at puremagic.com
Fri Oct 24 07:49:44 PDT 2014


Hello. I realize the wording "non-const method" is probably a C++-ism
but please see the following code. In both C++ and D, 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 ;
    Pair(1, 2).handle.x = 5 ;
}
--
nonconst.cpp:
--
struct Pair {
    int x, y ;
    Pair(int x, int y) : x(x), y(y) {}
    Pair & handle() { return *this ; }
} ;
int main () {
    Pair(1, 2).x = 5 ;
    Pair(1, 2).handle().x = 5 ;
}

-- 
Shriramana Sharma ஶ்ரீரமணஶர்மா श्रीरमणशर्मा



More information about the Digitalmars-d-learn mailing list