Re: unsigned to int assignment
On Mar 25, 4:17 am, John Doe <NOTOSPAMjohndoe64...@yahoo.com> wrote:
We all know unsigned ints may contain larger values than a common int
can hold. I want to know which parts of the standard cover the assigning
of unsigned ints to int and whether
int i = 2^32 - 1;
is legal, does not result in undefined behavior. Of course, calculate
2^32 yourselves first :)
(I presume by 2^32, you mean 2 raised to the 32nd, and not
literally 2^32, which is 34.)
The first thing to point out is that the value of 2^32 is not
necessarily legal in a C++ program; the largest value that an
implementation is required to support is 2^32-1. This will
change with the next version of the standard, however, where
implementations will be required to support at least 2^64-1.
The second point is that unless one of the values is specified
as octal or hexadecimal, or a U suffix is present, the
expression always has a signed integral type. If it doesn't fit
into any signed integral type, it is an error (and I think that
the compiler is required to diagnose it).
I rather suspect, however, that you were asking about something
like:
int i = std::numeric_limits< unsigned >::max() ;
In this case, the results are implementation defined; the C
standard makes it clear that one possible result is an
implementation defined signal.
Note that this is different than something like:
int i = std::numeric_limits< int >::max() ;
++ i ;
which is undefined behavior; the implementation can do anything,
including reformat your hard disk, and is not required to
document what it does.
--
James Kanze (Gabi Software) email: james.kanze@gmail.com
Conseils en informatique orient?e objet/
Beratung in objektorientierter Datenverarbeitung
9 place S?mard, 78210 St.-Cyr-l'?cole, France, +33 (0)1 30 23 00 34
--
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]