A new fundamental (builtin) type has recently been added to C++. It is a type for representing Boolean values and uses the keyword "bool". For example, you could say:
bool b; b = true; if (b) ...
A bool value is either true or false. A bool value can be converted to an integer:
bool b; int i; b = false; i = int(b);
in which case false turns into 0 and true into 1. This process goes under the C/C++ name of "integral promotion".
A pointer, integer, or enumeration can be converted to a bool. A null pointer or zero value becomes false, while any other value becomes true. Such conversion is required for conditional statements:
char* p; ... if (p) ...
In this example "p" is converted to bool and then the true/false value is checked to determine whether to execute the conditional block of code.
Why is a bool type an advantage? You can get a variety of opinions on whether this is a step forward. In C, common usage to mimic this type would be as follows:
typedef int Bool; #define FALSE 0 #define TRUE 1
One problem with such an approach is that it's not at all type-safe. For example, a programmer could say:
Bool b; b = 37;
and the compiler wouldn't care. Another problem is displaying values of Boolean type:
printf("%s", b ? "true" : "false");
which is awkward. In C++ it is possible to set up a stream I/O output operator specifically for a particular type, and thus output of bool values can be distinguished from plain integral types. This is an example of function overloading (see next section). Without bool as a distinct type, usage like:
void f(int i) {} void f(Bool b) {}
would be invalid.
Finally, why wasn't bool added to the language, but as a class type found in a standard library? This question is hard to answer, but one possible reason is that many C implementations have supplied a Boolean pseudo-type using a typedef and #define scheme as illustrated above, and these implementations rely on representing Booleans as integral types rather than as class types.
(further comment)
In the last issue we talked about the new fundamental type "bool". Two additional comments should be made about this feature. An example of how Boolean has been faked in C was given:
typedef int Bool; #define FALSE 0 #define TRUE 1
and then usage like this:
Bool b; b = 37;
was presented, with a comment that a C compiler would not complain. A C++ compiler given similar usage:
bool b; b = 37;
will not complain either, but the two sequences are not the same. In the C case, a later statement like:
if (b == TRUE) ...
will fail, because it reduces to:
if (37 == 1) ...
In the C++ case, the statement:
b = 37;
turns into:
b = true;
and a later test:
if (b == true) ...
will indeed succeed.
The issue was also raised as to why bool was not implemented as a class type in some C++ standard library. Dag Bruck of the ANSI/ISO C++ committees sent an example of why this will not work.
There is a rule in C++ that says that at most one user-defined conversion may be automatically applied. A user-defined conversion is a constructor like:
class A { public: A(int); };
to convert an int to an A, or a conversion function:
class A { public: operator int(); };
to convert an A to an int.
If bool is a class type, for example:
class bool { public: operator int(); };
then the call "f(3 < 4)" in this code:
class X { public: X(int); }; void f(X); main() { f(3 < 4); }
will result in two user-defined conversions, one to convert the bool class object resulting from "3 < 4" to an int, the other to call the X(int) constructor on the resulting int.
India seo freelance web designer India web development ecommerce website developer India
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100