Prev: invalid initialization of non-const reference of type
Next: Summary of C++0x Feature Availability
From: Olaf Klein on 5 Mar 2010 20:58 Dear group, is it possible to initialize an unsigned variable with its maximum value with the following lines? unsigned u = -1; std::size_t s = -1; I am asking because the standard does not guarantee overflow behavior of signed types while it does for unsigned types. What applies here? Olaf Klein -- [ See http://www.gotw.ca/resources/clcm.htm for info about ] [ comp.lang.c++.moderated. First time posters: Do this! ]
From: Daniel Krügler on 6 Mar 2010 08:15 On 6 Mrz., 14:58, Olaf Klein <olaf.klei...(a)googlemail.com> wrote: > is it possible to initialize an unsigned variable with its maximum > value with the following lines? > > unsigned u = -1; > std::size_t s = -1; > > I am asking because the standard does not guarantee overflow behavior > of signed types while it does for unsigned types. What applies here? This is clearly expressed in [basic.fundamental]/4 "Unsigned integers, declared unsigned, shall obey the laws of arithmetic modulo 2n where n is the number of bits in the value representation of that particular size of integer.45" with footnote 45: "This implies that unsigned arithmetic does not overflow because a result that cannot be represented by the resulting unsigned integer type is reduced modulo the number that is one greater than the largest value that can be represented by the resulting unsigned integer type." This are just different words for what you are trying to realize. HTH & Greetings from Bremen, Daniel Kr�gler -- [ See http://www.gotw.ca/resources/clcm.htm for info about ] [ comp.lang.c++.moderated. First time posters: Do this! ]
From: Bo Persson on 6 Mar 2010 08:15 Olaf Klein wrote: > Dear group, > > is it possible to initialize an unsigned variable with its maximum > value with the following lines? > > unsigned u = -1; > std::size_t s = -1; > > I am asking because the standard does not guarantee overflow > behavior of signed types while it does for unsigned types. What > applies here? > Well, these are unsigned, right? :-) The standard guarantees that u and s are both initialized to the largest value of their respective type. Bo Persson -- [ See http://www.gotw.ca/resources/clcm.htm for info about ] [ comp.lang.c++.moderated. First time posters: Do this! ]
From: Thomas Richter on 7 Mar 2010 05:01 Daniel Kr�gler wrote: > On 6 Mrz., 14:58, Olaf Klein <olaf.klei...(a)googlemail.com> wrote: >> is it possible to initialize an unsigned variable with its maximum >> value with the following lines? >> >> unsigned u = -1; >> std::size_t s = -1; >> >> I am asking because the standard does not guarantee overflow behavior >> of signed types while it does for unsigned types. What applies here? > > This is clearly expressed in [basic.fundamental]/4 > > "Unsigned integers, declared unsigned, shall obey the laws > of arithmetic modulo 2n where n is the number of bits in > the value representation of that particular size of integer.45" Is this really the right argument? After all, this is an implicit conversion from an int to an unsigned integral type, thus the arithmetic of unsigned types should not really matter here. After all, two types are involved here, not just one. So long, Thomas -- [ See http://www.gotw.ca/resources/clcm.htm for info about ] [ comp.lang.c++.moderated. First time posters: Do this! ]
From: Johannes Schaub (litb) on 7 Mar 2010 08:33 Thomas Richter wrote: > Daniel Krügler wrote: >> On 6 Mrz., 14:58, Olaf Klein <olaf.klei...(a)googlemail.com> wrote: >>> is it possible to initialize an unsigned variable with its maximum >>> value with the following lines? >>> >>> unsigned u = -1; >>> std::size_t s = -1; >>> >>> I am asking because the standard does not guarantee overflow behavior >>> of signed types while it does for unsigned types. What applies here? >> >> This is clearly expressed in [basic.fundamental]/4 >> >> "Unsigned integers, declared unsigned, shall obey the laws >> of arithmetic modulo 2n where n is the number of bits in >> the value representation of that particular size of integer.45" > > Is this really the right argument? After all, this is an implicit > conversion from an int to an unsigned integral type, thus the arithmetic > of unsigned types should not really matter here. After all, two types > are involved here, not just one. > I think i agree with you. For example, i believe this code is UB: unsigned int i = -1.0f; => "The behavior is undefined if the truncated value cannot be represented in the destination type." However, the following is defined: unsigned int i = -1; => "If the destination type is unsigned, the resulting value is the least unsigned integer congruent to the source integer (modulo 2n where n is the number of bits used to represent the unsigned type)." -- [ See http://www.gotw.ca/resources/clcm.htm for info about ] [ comp.lang.c++.moderated. First time posters: Do this! ]
|
Next
|
Last
Pages: 1 2 3 4 Prev: invalid initialization of non-const reference of type Next: Summary of C++0x Feature Availability |