Connecting Tech Pros Worldwide Forums | Help | Site Map

int - unsigned = ?

John Doe
Guest
 
Posts: n/a
#1: Mar 24 '07
Let's say I did this:

i -= u;

i is a signed int and u is an unsigned int. According to the c++
standard the rvalue result is going to be converted to unsigned int?
Then what? Where does it say the result can be assigned to an int?

— Otherwise, if either operand is unsigned, the other shall be converted
to unsigned.


Jack Klein
Guest
 
Posts: n/a
#2: Mar 24 '07

re: int - unsigned = ?


On Sat, 24 Mar 2007 02:05:51 +0100, John Doe
<NOTOSPAMjohndoe64738@yahoo.comwrote in comp.lang.c++:
Quote:
Let's say I did this:
>
i -= u;
>
i is a signed int and u is an unsigned int. According to the c++
standard the rvalue result is going to be converted to unsigned int?
No, the rvalue result will not be converted at all. 'i', the signed
int, will be converted to unsigned int, then the subtraction will be
performed, naturally yielding an unsigned int result automatically.
The conversion is on the operand, not the result.
Quote:
Then what? Where does it say the result can be assigned to an int?
Where does it say that it cannot? The C++ standard defines integer
functions clearly. The unsigned int result of the subtraction is
automatically converted to signed int by the assignment. If the value
of the unsigned int result is within the range of values that can be
represented in signed int, that value will be stored in the signed
int. If the value of the unsigned int is greater than INT_MAX, then
the value stored in 'i' is implementation-defined.
Quote:
— Otherwise, if either operand is unsigned, the other shall be converted
to unsigned.
The line you are quoting above says that the other operand is
converted, not the result.

--
Jack Klein
Home: http://JK-Technology.Com
FAQs for
comp.lang.c http://c-faq.com/
comp.lang.c++ http://www.parashift.com/c++-faq-lite/
alt.comp.lang.learn.c-c++
http://www.club.cc.cmu.edu/~ajo/docs/FAQ-acllc.html
John Doe
Guest
 
Posts: n/a
#3: Mar 24 '07

re: int - unsigned = ?


Jack Klein pravi:
Quote:
On Sat, 24 Mar 2007 02:05:51 +0100, John Doe
<NOTOSPAMjohndoe64738@yahoo.comwrote in comp.lang.c++:
Quote:
>Let's say I did this:
>>
>i -= u;
>>
>i is a signed int and u is an unsigned int. According to the c++
>standard the rvalue result is going to be converted to unsigned int?
No, the rvalue result will not be converted at all. 'i', the signed
int, will be converted to unsigned int, then the subtraction will be
performed, naturally yielding an unsigned int result automatically.
The conversion is on the operand, not the result.
yes
Quote:
Quote:
>Then what? Where does it say the result can be assigned to an int?
Where does it say that it cannot? The C++ standard defines integer
functions clearly. The unsigned int result of the subtraction is
automatically converted to signed int by the assignment. If the value
of the unsigned int result is within the range of values that can be
represented in signed int, that value will be stored in the signed
int. If the value of the unsigned int is greater than INT_MAX, then
the value stored in 'i' is implementation-defined.
Where does it say that it can? I was looking for a reference.
Quote:
Quote:
>— Otherwise, if either operand is unsigned, the other shall be converted
>to unsigned.
The line you are quoting above says that the other operand is
converted, not the result.
ok
Jack Klein
Guest
 
Posts: n/a
#4: Mar 26 '07

re: int - unsigned = ?


On Sat, 24 Mar 2007 17:04:27 +0100, John Doe
<NOTOSPAMjohndoe64738@yahoo.comwrote in comp.lang.c++:
Quote:
Jack Klein pravi:
Quote:
On Sat, 24 Mar 2007 02:05:51 +0100, John Doe
<NOTOSPAMjohndoe64738@yahoo.comwrote in comp.lang.c++:
Quote:
Let's say I did this:
>
i -= u;
>
i is a signed int and u is an unsigned int. According to the c++
standard the rvalue result is going to be converted to unsigned int?
No, the rvalue result will not be converted at all. 'i', the signed
int, will be converted to unsigned int, then the subtraction will be
performed, naturally yielding an unsigned int result automatically.
The conversion is on the operand, not the result.
>
yes
>
Quote:
Quote:
Then what? Where does it say the result can be assigned to an int?
Where does it say that it cannot? The C++ standard defines integer
functions clearly. The unsigned int result of the subtraction is
automatically converted to signed int by the assignment. If the value
of the unsigned int result is within the range of values that can be
represented in signed int, that value will be stored in the signed
int. If the value of the unsigned int is greater than INT_MAX, then
the value stored in 'i' is implementation-defined.
>
Where does it say that it can? I was looking for a reference.
Paragraph 3 of 4.7, "Integral conversions".

--
Jack Klein
Home: http://JK-Technology.Com
FAQs for
comp.lang.c http://c-faq.com/
comp.lang.c++ http://www.parashift.com/c++-faq-lite/
alt.comp.lang.learn.c-c++
http://www.club.cc.cmu.edu/~ajo/docs/FAQ-acllc.html
Closed Thread