bool ?
-
Plotzenhotz schrieb:
Äh.
Blubb.
Bin mir ziemlich sicher dass im C++ Standard (sinngemäss) steht dass ein Byte >= 8 Bit haben muss, die kleinste adressierbare Einheit der Maschine darstellt, und dass "char" genau ein solches Byte belegt.Steht dahier im übrigen auch so:
http://www.parashift.com/c++-faq-lite/newbie.html#faq-29.5Dann ist das wieder so eine von C geerbte Garantie (wie zum Beispiel auch die Mindestgröße von int).
Kuldren schrieb:
Im Nachhinein betrachtet: Echt seltsam dass in irgendeinem uralten Header
TRUE und FALSE definiert sind...Nö, bool ist eben erst sehr spät als Built-in in die Sprache reingekommen.
-
Ich hab vom Standard nur nen Draft von 96.
3.9.1 Fundamental types [basic.fundamental]
1 Objects declared as characters (char) shall be large enough to store any member of the implementation’s
basic character set. If a character from this set is stored in a character object, the integral value of that character
object is equal to the value of the single character literal form of that character. It is implementationdefined
whether a char object can hold negative values. Characters can be explicitly declared unsigned
or signed. Plain char, signed char, and unsigned char are three distinct types. A char, a
signed char, and an unsigned char occupy the same amount of storage and have the same alignment
requirements (3.9); 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 a
signed char or an unsigned char; which one is implementationdefined.2 There are four signed integer types: “signed char”, “short int”, “int”, and “long int.” In this
list, each type provides at least as much storage as those preceding it in the list. Plain ints have the natural
size suggested by the architecture of the execution environment37) ; the other signed integer types are
provided to meet special needs.
__________________
37) that is, large enough to contain any value in the range of INT_MIN and INT_MAX, as defined in the header <climits>.3– 32 Basic concepts DRAFT: 2 December 1996 3.9.1 Fundamental types
3 For each of the signed integer types, there exists a corresponding (but different) unsigned integer type:
“unsigned char”, “unsigned short int”, “unsigned int”, and “unsigned long
int,” each of which occupies the same amount of storage and has the same alignment requirements (3.9)
as the corresponding signed integer type38) ; that is, each signed integer type has the same object representation
as its corresponding unsigned integer type. The range of nonnegative values of a signed integer type
is a subrange of the corresponding unsigned integer type, and the value representation of each corresponding
signed/unsigned type shall be the same.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.39)5 Type wchar_t is a distinct type whose values can represent distinct codes for all members of the largest
extended character set specified among the supported locales (22.1.1). Type wchar_t shall have the same
size, signedness, and alignment requirements (1.6) as one of the other integral types, called its underlying
type.6 Values of type bool are either true or false.40) [Note: there are no signed, unsigned, short, or
long bool types or values. ] As described below, bool values behave as integral types. Values of type
bool participate in integral promotions (4.5).7 Types bool, char, wchar_t, and the signed and unsigned integer types are collectively called integral
types.41) A synonym for integral type is integer type. The representations of integral types shall define values
by use of a pure binary numeration system.42) [Example: this International Standard permits 2’s complement,
1’s complement and signed magnitude representations for integral types. ]8 There are three floating point types: float, double, and long double. The type double provides
at least as much precision as float, and the type long double provides at least as much precision as
double. The set of values of the type float is a subset of the set of values of the type double; the set
of values of the type double is a subset of the set of values of the type long double. The value representation
of floatingpoint
types is implementationdefined.
Integral and floating types are collectively
called arithmetic types. Specializations of the standard template numeric_limits (18.2) shall specify
the maximum and minimum values of each arithmetic type for an implementation.9 The void type has an empty set of values. The void type is an incomplete type that cannot be completed.
It is used as the return type for functions that do not return a value. Any expression can be explicitly converted
to type cv void (5.4). An expression of type void shall be used only as an expression statement
(6.2), as an operand of a comma expression (5.18), or as a second or third operand of ?: (5.16).10 [Note: even if the implementation defines two or more basic types to have the same value representation,
they are nevertheless different types. ]Zumindest da kann ich nirgendwo irgendwelche Garantien bezüglich Bytes und Bits entdecken.
Gruß Spacelord
-
OK, hab jetzt nachgesehen, im Standard ist (für mich) nix zu finden was auf 8 Bit schliessen lässt. Ich vermute mal es ist aber vernünftig vorauszusetzen dass immer char >= 8 Bit, short & int >= 16 Bit und long >= 32 Bit. Auf diversen "exotischen" Plattformen die vielleicht ein native 7-Bit word haben wird man wohl eher nen C-Compiler antreffen als nen C++-Compiler, von daher denke ich kann man diese ignorieren

Wie schön und einfach ist dagegen Java was das Thema angeht

-
Kuldren schrieb:
Also laut meinem Buch hat bool 1 Byte, genau wie char...
Was char betrifft, ist das korrekt. Bei bool ist das allerdings nicht so. Der Standard legt lediglich fest, dass bool ein integraler Typ ist. Wenn ein Compiler also Lust hat, kann er bool intern auch auf long abbilden.
Kuldren schrieb:
Ich werde jetzt nur mehr true und false verwenden ohne typedef/define...
Um's kurz zu machen, das ist auf jeden Fall die richtige Entscheidung. Und mehr gibt's dazu eigentlich auch nicht zu sagen.