README
A "trap value", or "trap representation" for typeT
, is a bit combination (of the underlying storage) that yields an invalid value ofT.
Trying to interpret the representation of an invalid value will cause undefined behavior.
Let the battle begin..
Another question has started a heated discussion regarding char
, and the possibility of an implementation having trap representations for it.
Question:
- Can
char
possibly have trap values?
Quotes that has been mentioned in the previous discussion:
These sections are the most quoted ones during the previous argumentation, are they contradicting?
3.9.1p1
Fundamental types[basic.fundamental]
It is implementation-defined whether a
char
can hold negative values. Characters can be explicitly declaredsigned
orunsigned
.A
char,
asigned char,
and anunsigned char
occupy the same amount of storage and have the same alignment requirements (3.11); that is, they have the same object representation. For character types, all bits of the object representation participate in the value representation.For unsigned character types, all possible bit patterns of the value representation represent numbers. These requirements do not hold for other types.
In any particular implementation, a plain
char
object can take on either the same values as asigned char
or anunsigned char;
which one is implementation-defined.
3.9p2 Types
[basic.types]
For any object (other than a base-class subobject) of trivially copyable type
T,
whether or not the object holds a valid value of typeT
, the underlying bytes (1.7) making up the object can be copied into an array ofchar
orunsigned char.
If the content of the array of
char
orunsigned char
is copied back into the object, the object shall subsequently hold its original value.