www.digitalmars.com         C & C++   DMDScript  

digitalmars.D - Why isn't ++x an lvalue in D?

reply "Bill Baxter" <wbaxter gmail.com> writes:
Another thread just reminded me of something I use frequently in C++
that doesn't work in D because ++x is not an lvalue:

   int x,N;
  ...
   ++x %= N;

So is there some deep reason for not making it an lvalue like in C++?

--bb
Jan 08 2009
next sibling parent reply Weed <resume755 mail.ru> writes:
Bill Baxter :
 Another thread just reminded me of something I use frequently in C++
 that doesn't work in D because ++x is not an lvalue:
 
    int x,N;
   ...
    ++x %= N;
 
 So is there some deep reason for not making it an lvalue like in C++?
 

++x is x+=1 in D: void main() { int i =3; int N =2; (i+=1) %= N; } Error: i += 1 is not an lvalue. C++: int main() { int i = 2; int N = 3; i+1 %= N; return 0; } error: lvalue required as left operand of assignment
Jan 08 2009
parent reply Weed <resume755 mail.ru> writes:
Bill Baxter пишет:
 2009/1/9 Weed <resume755 mail.ru>:
 Bill Baxter пишет:
 Another thread just reminded me of something I use frequently in C++
 that doesn't work in D because ++x is not an lvalue:

    int x,N;
   ...
    ++x %= N;

 So is there some deep reason for not making it an lvalue like in C++?

void main() { int i =3; int N =2; (i+=1) %= N; } Error: i += 1 is not an lvalue. C++: int main() { int i = 2; int N = 3; i+1 %= N; return 0; } error: lvalue required as left operand of assignment

What does C++ do if you use (i+=1) %= N instead of (i+1)? Doesn't += also return an lvalue in C++?

I am a bit mixed, but the meaning has not changed: $ cat demo.cpp int main() { int i = 2; int N = 3; i+=1 %= N; return 0; } $ c++ demo.cpp demo.cpp: In function ‘int main()’: demo.cpp:5: error: lvalue required as left operand of assignment
Jan 08 2009
next sibling parent reply Weed <resume755 mail.ru> writes:
Weed пишет:
 Bill Baxter пишет:
 2009/1/9 Weed <resume755 mail.ru>:
 Bill Baxter пишет:
 Another thread just reminded me of something I use frequently in C++
 that doesn't work in D because ++x is not an lvalue:

    int x,N;
   ...
    ++x %= N;

 So is there some deep reason for not making it an lvalue like in C++?

void main() { int i =3; int N =2; (i+=1) %= N; } Error: i += 1 is not an lvalue. C++: int main() { int i = 2; int N = 3; i+1 %= N; return 0; } error: lvalue required as left operand of assignment

also return an lvalue in C++?

I am a bit mixed, but the meaning has not changed: $ cat demo.cpp int main() { int i = 2; int N = 3; i+=1 %= N; return 0; } $ c++ demo.cpp demo.cpp: In function ‘int main()’: demo.cpp:5: error: lvalue required as left operand of assignment

And I think it is wrong that the ++ same as += 1. The operator ++ in C uniquely compiles in CPU instruction "increment". For objects it would be better to make a += 1 only if undefined overloaded operator ++.
Jan 08 2009
parent reply Weed <resume755 mail.ru> writes:
Bill Baxter :
 On Fri, Jan 9, 2009 at 2:43 PM, Weed <resume755 mail.ru> wrote:
 Weed :
 Bill Baxter :
 2009/1/9 Weed <resume755 mail.ru>:
 Bill Baxter :
 Another thread just reminded me of something I use frequently in C++
 that doesn't work in D because ++x is not an lvalue:

    int x,N;
   ...
    ++x %= N;

 So is there some deep reason for not making it an lvalue like in C++?

void main() { int i =3; int N =2; (i+=1) %= N; } Error: i += 1 is not an lvalue. C++: int main() { int i = 2; int N = 3; i+1 %= N; return 0; } error: lvalue required as left operand of assignment

also return an lvalue in C++?

$ cat demo.cpp int main() { int i = 2; int N = 3; i+=1 %= N; return 0; } $ c++ demo.cpp demo.cpp: In function 'int main()': demo.cpp:5: error: lvalue required as left operand of assignment

uniquely compiles in CPU instruction "increment". For objects it would be better to make a += 1 only if undefined overloaded operator ++.

Yeh, I think that's scheduled to be changed after Andrei's repeated thrashings of Walter.

Thus, once it works it is necessary to change anything if only for the real types increment will give increase for the lowest possible value. (I do not know how the CPU instruction increment works for real values)
Jan 08 2009
parent Don <nospam nospam.com> writes:
Weed wrote:
 Bill Baxter :
 On Fri, Jan 9, 2009 at 2:43 PM, Weed <resume755 mail.ru> wrote:
 Weed :
 Bill Baxter :
 2009/1/9 Weed <resume755 mail.ru>:
 Bill Baxter :
 Another thread just reminded me of something I use frequently in C++
 that doesn't work in D because ++x is not an lvalue:

    int x,N;
   ...
    ++x %= N;

 So is there some deep reason for not making it an lvalue like in C++?

void main() { int i =3; int N =2; (i+=1) %= N; } Error: i += 1 is not an lvalue. C++: int main() { int i = 2; int N = 3; i+1 %= N; return 0; } error: lvalue required as left operand of assignment

also return an lvalue in C++?

$ cat demo.cpp int main() { int i = 2; int N = 3; i+=1 %= N; return 0; } $ c++ demo.cpp demo.cpp: In function 'int main()': demo.cpp:5: error: lvalue required as left operand of assignment

uniquely compiles in CPU instruction "increment". For objects it would be better to make a += 1 only if undefined overloaded operator ++.

thrashings of Walter.

Thus, once it works it is necessary to change anything if only for the real types increment will give increase for the lowest possible value. (I do not know how the CPU instruction increment works for real values)

There is none. In tango.math.Math there's nextFloatUp, nextDoubleUp, nextRealUp, which perform the "next possible number" operation. In D, ++x for real x performs x = x + 1.0. If x is (say) 1e80, then (++x) == x ! I'm not sure why D allows ++x for floating-point types, it's asking for trouble IMHO -- if you're incrementing a real, I think it's highly probable that you have a bug somewhere.
Jan 09 2009
prev sibling next sibling parent reply "Nick Sabalausky" <a a.a> writes:
"Weed" <resume755 mail.ru> wrote in message 
news:gk6n29$218m$1 digitalmars.com...
 Bill Baxter ?????:
 2009/1/9 Weed <resume755 mail.ru>:
 Bill Baxter ?????:
 Another thread just reminded me of something I use frequently in C++
 that doesn't work in D because ++x is not an lvalue:

    int x,N;
   ...
    ++x %= N;

 So is there some deep reason for not making it an lvalue like in C++?

void main() { int i =3; int N =2; (i+=1) %= N; } Error: i += 1 is not an lvalue. C++: int main() { int i = 2; int N = 3; i+1 %= N; return 0; } error: lvalue required as left operand of assignment

What does C++ do if you use (i+=1) %= N instead of (i+1)? Doesn't += also return an lvalue in C++?

I am a bit mixed, but the meaning has not changed: $ cat demo.cpp int main() { int i = 2; int N = 3; i+=1 %= N;

Shouldn't that be "(i += 1) %= N"? Assignments are right-associative (but then these are op-assigns...). The error is probably saying that "1" can't be an lvalue for "%= N" which, of course, is true.
 return 0;
 }

 $ c++ demo.cpp
 demo.cpp: In function 'int main()':
 demo.cpp:5: error: lvalue required as left operand of assignment

Jan 08 2009
parent Weed <resume755 mail.ru> writes:
Nick Sabalausky :
 "Weed" <resume755 mail.ru> wrote in message 
 news:gk6n29$218m$1 digitalmars.com...
 Bill Baxter ?????:
 2009/1/9 Weed <resume755 mail.ru>:
 Bill Baxter ?????:
 Another thread just reminded me of something I use frequently in C++
 that doesn't work in D because ++x is not an lvalue:

    int x,N;
   ...
    ++x %= N;

 So is there some deep reason for not making it an lvalue like in C++?

void main() { int i =3; int N =2; (i+=1) %= N; } Error: i += 1 is not an lvalue. C++: int main() { int i = 2; int N = 3; i+1 %= N; return 0; } error: lvalue required as left operand of assignment

also return an lvalue in C++?

$ cat demo.cpp int main() { int i = 2; int N = 3; i+=1 %= N;

Shouldn't that be "(i += 1) %= N"? Assignments are right-associative (but then these are op-assigns...). The error is probably saying that "1" can't be an lvalue for "%= N" which, of course, is true.

Yes, my code is wrong And apparently, we have an error in D
 
 return 0;
 }

 $ c++ demo.cpp
 demo.cpp: In function 'int main()':
 demo.cpp:5: error: lvalue required as left operand of assignment


Jan 08 2009
prev sibling parent Weed <resume755 mail.ru> writes:
Bill Baxter :
 On Fri, Jan 9, 2009 at 2:22 PM, Weed <resume755 mail.ru> wrote:
 Bill Baxter :
 2009/1/9 Weed <resume755 mail.ru>:
 Bill Baxter :
 Another thread just reminded me of something I use frequently in C++
 that doesn't work in D because ++x is not an lvalue:

    int x,N;
   ...
    ++x %= N;

 So is there some deep reason for not making it an lvalue like in C++?

void main() { int i =3; int N =2; (i+=1) %= N; } Error: i += 1 is not an lvalue. C++: int main() { int i = 2; int N = 3; i+1 %= N; return 0; } error: lvalue required as left operand of assignment

also return an lvalue in C++?

$ cat demo.cpp int main() { int i = 2; int N = 3; i+=1 %= N; return 0; } $ c++ demo.cpp demo.cpp: In function 'int main()': demo.cpp:5: error: lvalue required as left operand of assignment

Huh. Ok, well I guess that answers it then. Thanks. Mystery solved! To summarize, In D ++x is x+=1, and in D, like in C++, x+=1 is not an lvalue. Got it. But now I wonder why it's not an lvalue in C++ or D. If x=y is an lvalue and ++x is an lvalue, why shouldn't x+=1 be one too?

It is also a lvalue, it is my mistake again. (i+=1) %= N;
Jan 08 2009
prev sibling next sibling parent "Bill Baxter" <wbaxter gmail.com> writes:
MjAwOS8xLzkgV2VlZCA8cmVzdW1lNzU1QG1haWwucnU+Ogo+IEJpbGwgQmF4dGVyINDJ28XUOgo+
PiBBbm90aGVyIHRocmVhZCBqdXN0IHJlbWluZGVkIG1lIG9mIHNvbWV0aGluZyBJIHVzZSBmcmVx
dWVudGx5IGluIEMrKwo+PiB0aGF0IGRvZXNuJ3Qgd29yayBpbiBEIGJlY2F1c2UgKyt4IGlzIG5v
dCBhbiBsdmFsdWU6Cj4+Cj4+ICAgIGludCB4LE47Cj4+ICAgLi4uCj4+ICAgICsreCAlPSBOOwo+
Pgo+PiBTbyBpcyB0aGVyZSBzb21lIGRlZXAgcmVhc29uIGZvciBub3QgbWFraW5nIGl0IGFuIGx2
YWx1ZSBsaWtlIGluIEMrKz8KPj4KPgo+ICsreCBpcyB4Kz0xIGluIEQ6Cj4KPiB2b2lkIG1haW4o
KSB7Cj4gIGludCBpID0zOwo+ICBpbnQgTiA9MjsKPiAgKGkrPTEpICU9IE47Cj4gfQo+Cj4KPiBF
cnJvcjogaSArPSAxIGlzIG5vdCBhbiBsdmFsdWUuCj4KPiBDKys6Cj4KPiBpbnQgbWFpbigpCj4g
ewo+ICBpbnQgaSA9IDI7Cj4gIGludCBOID0gMzsKPiAgaSsxICU9IE47Cj4KPiAgcmV0dXJuIDA7
Cj4gfQo+Cj4gZXJyb3I6IGx2YWx1ZSByZXF1aXJlZCBhcyBsZWZ0IG9wZXJhbmQgb2YgYXNzaWdu
bWVudAo+CgpXaGF0IGRvZXMgQysrIGRvIGlmIHlvdSB1c2UgIChpKz0xKSAlPSBOIGluc3RlYWQg
b2YgKGkrMSk/ICBEb2Vzbid0ICs9CmFsc28gcmV0dXJuIGFuIGx2YWx1ZSBpbiBDKys/CgotLWJi
Cg==
J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