operator += on short int values - strange compiler warning

H

Henryk

I have some very simple code fragment:

short int n1, n2;
....
n1 += n2;

I get an warning from VS2003 - VS2005: warning C4244: '+=' : conversion
from 'int' to 'short', possible loss of data

n1 = n1 + n2;

Here I don't get the warning. Strange...

I'm using warning level 4. Is this "VC++ specific" or conformant to the
standard?

Greets
 
R

Roland Pibinger

I have some very simple code fragment:

short int n1, n2;
...
n1 += n2;

I get an warning from VS2003 - VS2005: warning C4244: '+=' : conversion
from 'int' to 'short', possible loss of data

n1 = n1 + n2;

Here I don't get the warning. Strange...

I'm using warning level 4. Is this "VC++ specific" or conformant to the
standard?

I would have expected the warning in both cases. The result of n1 + n2
is of type int according to the type promotion rules.

Best wishes,
Roland Pibinger
 
V

Victor Bazarov

Duane said:

I guess I don't understand the question. Why what? Why the promotions
are performed on both operands? Because the Standard says so (5/9). Or
why is it so in the Standard? I am not sure, ask in comp.std.c++.

V
 
D

Duane Hebert

I guess I don't understand the question. Why what? Why the promotions
are performed on both operands? Because the Standard says so (5/9). Or
why is it so in the Standard? I am not sure, ask in comp.std.c++.

Sorry for the unclear question. I wanted to know if the standard required
addition of two shorts to be promoted to an int. I know if the two are
different
sizes, they're promoted to the size of the larger.

Thanks.
 
V

Victor Bazarov

Duane said:
Sorry for the unclear question. I wanted to know if the standard required
addition of two shorts to be promoted to an int.

Yes, that's how I read 5/9, the fourth bullet item.
> I know if the two are
different
sizes, they're promoted to the size of the larger.

The Standard does not make it conditional. Both operands shall be
promoted. That means if 'int' can accommodate them, they will be made
'int'. If 'int' can't accommodate either of them, that one will be made
'unsigned int'. Then if one of them is 'unsigned' and the other isn't,
they are both made 'unsigned'.

V
 
D

Duane Hebert

The Standard does not make it conditional. Both operands shall be
promoted. That means if 'int' can accommodate them, they will be made
'int'. If 'int' can't accommodate either of them, that one will be made
'unsigned int'. Then if one of them is 'unsigned' and the other isn't,
they are both made 'unsigned'.

Thanks.
 
H

Henryk

Thank you all,

I thought that the promotion occures to the largest type of all
operands involved.

The question is why I do get this warning only for the += (and possibly
for -= and others too) but not for the explizit operations.

I did not use a pendatic warning level before, but for the current
project I have to. So I was not aware of this problem.

This project contains a lot of algorithm stuff with mathematical
operations. The compiler would have to produce tons of warnings
according to the promotion rules. But I only get a few from this += ...

Btw, is there a good online source for the C++ Standard? I just
downloaded a pdf from open-std.org but it seems not up-to-date (its
from 2001).
 
M

Marcelo Pinto

Victor said:
Yes, that's how I read 5/9, the fourth bullet item.

But the fourth bullet directs to 4.5, and 4.5/1 reads:
"An rvalue of type char, signed char, unsigned char, short int, or
unsigned short int **can** be converted to an rvalue of type int if int
can represent all the values of the source type; otherwise, the source
rvalue **can** be converted to an rvalue of type unsigned int."
(the ** are mine)

As I interpret it, it is not *required* it is a possibility. Please,
correct me if I am wrong.

[snip]

TIA,

Marcelo Pinto
 
V

Victor Bazarov

Marcelo said:
Victor Bazarov wrote:




But the fourth bullet directs to 4.5, and 4.5/1 reads:
"An rvalue of type char, signed char, unsigned char, short int, or
unsigned short int **can** be converted to an rvalue of type int if int
can represent all the values of the source type; otherwise, the source
rvalue **can** be converted to an rvalue of type unsigned int."
(the ** are mine)

As I interpret it, it is not *required* it is a possibility. Please,
correct me if I am wrong.

Subclause 4.5 shows _how_ it's done *if* it's done. 5/9 requires that it
_shall_ be done. Read it again.

V
 
V

Victor Bazarov

Henryk said:
[..]
Btw, is there a good online source for the C++ Standard? I just
downloaded a pdf from open-std.org but it seems not up-to-date (its
from 2001).

Please look in the FAQ.

V
 
M

Marcelo Pinto

Victor Bazarov escreveu:
Subclause 4.5 shows _how_ it's done *if* it's done. 5/9 requires that it
_shall_ be done. Read it again.

V
--

Yes, I see it now, thank you.

Marcelo Pinto
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,744
Messages
2,569,484
Members
44,903
Latest member
orderPeak8CBDGummies

Latest Threads

Top