Connecting Tech Pros Worldwide Help | Site Map

int - unsigned = ?

 
LinkBack Thread Tools Search this Thread
  #1  
Old March 24th, 2007, 01:15 AM
John Doe
Guest
 
Posts: n/a
Default int - unsigned = ?

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, 04:25 AM
Jack Klein
Guest
 
Posts: n/a
Default 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, 04:05 PM
John Doe
Guest
 
Posts: n/a
Default 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, 01:45 AM
Jack Klein
Guest
 
Posts: n/a
Default 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
 

Bookmarks

Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On

Popular Articles

What is Bytes?

We are a network of experts and professionals in IT and software development that help one another with answers to tough questions and share insights. Get the best answers to your questions from over 220,662 network members.