Connecting Tech Pros Worldwide Help | Site Map

int - unsigned = ?

  #1  
Old March 24th, 2007, 02:15 AM
John Doe
Guest
 
Posts: n/a
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.

  #2  
Old March 24th, 2007, 05:25 AM
Jack Klein
Guest
 
Posts: n/a

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
  #3  
Old March 24th, 2007, 05:05 PM
John Doe
Guest
 
Posts: n/a

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
  #4  
Old March 26th, 2007, 02:45 AM
Jack Klein
Guest
 
Posts: n/a

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


Similar Threads
Thread Thread Starter Forum Replies Last Post
qsorting type int** beagle197@hotmail.com answers 8 March 2nd, 2007 03:25 PM
'int *i ' or 'int* i' Julia answers 16 July 23rd, 2006 08:05 PM
regarding typedef yang__lee@ausi.com answers 14 March 29th, 2006 07:55 AM
set<const int> - does not compile chrisstankevitz@yahoo.com answers 4 December 9th, 2005 07:05 PM
need help with large int multiplication and division akickdoe22@hotmail.com answers 1 July 23rd, 2005 12:54 AM