| Thread overview | ||||||||||||||
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
|
January 09, 2009 Why isn't ++x an lvalue in D? | ||||
|---|---|---|---|---|
| ||||
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 | ||||
January 09, 2009 Re: Why isn't ++x an lvalue in D? | ||||
|---|---|---|---|---|
| ||||
Posted in reply to Bill Baxter | 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
| |||
January 09, 2009 Re: Why isn't ++x an lvalue in D? | ||||
|---|---|---|---|---|
| ||||
Posted in reply to Weed | 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++?
>>
>
> ++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
>
What does C++ do if you use (i+=1) %= N instead of (i+1)? Doesn't +=
also return an lvalue in C++?
--bb
| |||
January 09, 2009 Re: Why isn't ++x an lvalue in D? | ||||
|---|---|---|---|---|
| ||||
Posted in reply to Bill Baxter | 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++?
>>>
>> ++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
>>
>
> 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
| |||
January 09, 2009 Re: Why isn't ++x an lvalue in D? | ||||
|---|---|---|---|---|
| ||||
Posted in reply to Weed | 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++?
>>>>
>>> ++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
>>>
>> 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
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 ++.
| |||
January 09, 2009 Re: Why isn't ++x an lvalue in D? | ||||
|---|---|---|---|---|
| ||||
Posted in reply to Weed | 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++?
>>>>
>>> ++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
>>>
>>
>> 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
>
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?
--bb
| |||
January 09, 2009 Re: Why isn't ++x an lvalue in D? | ||||
|---|---|---|---|---|
| ||||
Posted in reply to Weed | 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++?
>>>>>
>>>> ++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
>>>>
>>> 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
>
> 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 ++.
Yeh, I think that's scheduled to be changed after Andrei's repeated thrashings of Walter.
--bb
| |||
January 09, 2009 Re: Why isn't ++x an lvalue in D? | ||||
|---|---|---|---|---|
| ||||
Posted in reply to Weed | "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++? >>>> >>> ++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 >>> >> >> 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 | |||
January 09, 2009 Re: Why isn't ++x an lvalue in D? | ||||
|---|---|---|---|---|
| ||||
Posted in reply to Bill Baxter | 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++?
>>>>>
>>>> ++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
>>>>
>>> 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
>>
>
> 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;
| |||
January 09, 2009 Re: Why isn't ++x an lvalue in D? | ||||
|---|---|---|---|---|
| ||||
Posted in reply to Nick Sabalausky | 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++? >>>>> >>>> ++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 >>>> >>> 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. 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 > > > | |||
Copyright © 1999-2021 by the D Language Foundation
Permalink
Reply