Why is (1) UB and (2) not?
char x; char y = x; // (1) unsigned char x; unsigned char y = x; // (2) Information taken from here.
Why is (1) UB and (2) not?
char x; char y = x; // (1) unsigned char x; unsigned char y = x; // (2) Information taken from here.
Currently there is a discrepancy between the C standard and the C ++ standard.
The standard language C is written (6.2.6 Representations of types)
5 Certain object representations need not represent a value of the object type. It is not clear. It is a fact that it modifies the pattern.
The so-called character type ( character type ) is defined in standard C as follows (6.2.5 Types):
15 The three types of char, signed char, and unsigned char are collectively called the character types. Concept of signed char or unsigned char
It is interesting to note that in the C ++ standard there is no definition of the term character type , although this term is used and it implies a similar definition as the generic name of the types char , signed char and unsigned char .
So, the C standard says about undefined behavior when an object does not have (any) character type.
In the C ++ 2014 standard, as I understand it, at the Straustrup initiative, we decided to clarify in particular, and this provision on undefined behavior regarding character types. They indicated that the only exception is unsigned char
12 If no initializer is specified for an object, the object is default-initialized. It is a value that has been defined as the value of the object. [Note: Objects with static or thread storage are zero-initialized, see 3.6.2. - an end note], it is not clear in the following cases:
....
(12.3) - It can be defined as a symbol.
I think that this clarification in the C ++ standard 2014 is due to the fact that for some systems of representing integer values for individual hardware architectures for the signed character type, a so-called trap representation is also available. For example, a negative zero can be such a representation, not valid for the representation of valid numbers.
Source: https://ru.stackoverflow.com/questions/599343/
All Articles
unsigned charexception - gender