-1

I do apologize if this question has been answered somewhere else, but I've searched, and have not yet found an answer...

I get the following warning when compiling the code below:

warning: conversion to 'short unsigned int' from 'int' may alter its value

Here is an excerpt of code (several examples to illustrate my question):

std::vector<unsigned short int> list = {1}; 
unsigned short int one = 1;

one += list.at(0);                            // produces warning
one += 1;                                     // produces warning
one += static_cast<unsigned short int> 1;     // produces warning
one++;                                        // does not produce warning

I've also tried other forms of arithmetic besides addition. Why does the compiler throw this warning, claiming that I'm converting to an 'unsigned short int' from an 'int' (especially when I've explicitly cast it as unsigned)? It would also seem, that for the second case, 'one += 1;', since the right side of the expression is a positive number, the compiler wouldn't have any problem adding it to the unsigned variable 'one'.

Also, the final test, 'one++;' does not produce a warning, and I'm not sure why.

I'm still getting used to asking questions on here, so forgive me if this question is trivial or unclear. Thanks!

  • 2
    I dont get a warning with g++, what flags are you using? – Fantastic Mr Fox Jul 11 '14 at 04:43
  • Agree no warnings `c++ -Wall`. Maybe paste the exact code. – spinkus Jul 11 '14 at 04:45
  • I can't see any reason for a warning (you'll get an error with parenthesis around the `1` you static cast though)... no warnings evident with [GCC on ideone](http://ideone.com/CKmX9L) but I'm not sure what warning levels that uses by default. Which compiler are you using, what settings? – Tony Delroy Jul 11 '14 at 04:47
  • With g++ it only [pops up](http://coliru.stacked-crooked.com/a/7a2c3223513b9772) if you specify `-Wconversion`. – T.C. Jul 11 '14 at 04:50
  • **-1** not the real code, the expression `one += static_cast 1;` would not compile. – Cheers and hth. - Alf Jul 11 '14 at 04:52
  • Actually I just found that warning is not turned by `-Wall` with g++. You need `-Wsign-conversion`, but I still don't get it with the above code (after syntax error is fixed). If you change the `unsigned short` to `int` you get it. – spinkus Jul 11 '14 at 05:03

1 Answers1

1

C++11 §5.17/7:


The behavior of an expression of the form E1 op = E2 is equivalent to E1 = E1 op E2 except that E1 is evaluated only once

This means that e.g.

one += list.at(0);

is evaluated as if it were

one = one + list.at(0);

except that one is evaluated only once.

In the expresseion one + list.at(0) both operands are first promoted to int, by C++11 §5/9, the usual arithmetic conversions rule.

In short, C++ binary arithmetic operators do not deal with operands of types with potentially smaller range than int. The operands are promoted. And then for the final assignment, there is (logical) conversion back down.

Cheers and hth. - Alf
  • 142,714
  • 15
  • 209
  • 331