[Date Prev][Date Next] [Thread Prev][Thread Next] [Date Index] [Thread Index]

Re: Is this OK in C++ and C?



On 12/31/2012 7:30 PM, Joe Pfeiffer wrote:
Jari Fredriksson <jarif@iki.fi> writes:

31.12.2012 20:33, Zbigniew Komarnicki kirjoitti:
Is this OK or is this a bug, when the wariable 'n' is
initializing by negative value? There no any warning.
Is this normal? I know that value -5 is converted
to unsigned but probably this should by printed a warning,
when this is a constant value. What do you think about this?


// prog.cpp
#include <iostream>
using namespace std;

int main()
{
const unsigned int n = -5;

   cout << "The variable n is: " << n << endl;

   return 0;
}

Results:
$ g++ -Wall -W  prog.cpp -o prog
$ ./prog
The variable n is: 4294967291

Thank you.


This is a known bug in Debian GNU/Linux. Happy new year ;)

Where does the standard require a warning in this case?  If no warning
is required, the behavior is not a bug.



I don't have a copy of the standard any more, but IIRC it does need a warning. It requires a conversion from signed to unsigned, which can cause incorrect data (as in this case).

Promotions (i.e. short to int, float to double) will never cause a loss of data, and do not require warnings.

But I also admit it's been several years since I looked at the spec and my memory isn't what it used to be (at least that's what my wife tells me).


Reply to: