mirror of
https://gitlab.com/fbb-git/cppannotations
synced 2024-11-16 07:48:44 +01:00
777b182edd
This allowed me to standardize the sourcetar and sf/* scripts: the base directory (containing ./git) is now empty, except for maintenance scripts, while the source files and build scripts of the annotations are stored in a subdirectory of their own.
54 lines
2.6 KiB
Text
54 lines
2.6 KiB
Text
The type tt(bool) represents boolean (logical) values, for which the (now
|
|
reserved) constants ti(true) and ti(false) may be used. Except for these
|
|
reserved values, integral values may also be assigned to variables of type
|
|
tt(bool), which are then implicitly converted to tt(true) and tt(false)
|
|
according to the following i(conversion rules) (assume tt(intValue) is an
|
|
tt(int)-variable, and tt(boolValue) is a tt(bool)-variable):
|
|
verb(
|
|
// from int to bool:
|
|
boolValue = intValue ? true : false;
|
|
|
|
// from bool to int:
|
|
intValue = boolValue ? 1 : 0;
|
|
)
|
|
Furthermore, when tt(bool) values are inserted into streams then tt(true)
|
|
is represented by tt(1), and tt(false) is represented by tt(0). Consider the
|
|
following example:
|
|
verb(
|
|
cout << "A true value: " << true << "\n"
|
|
"A false value: " << false << '\n';
|
|
)
|
|
The tt(bool) data type is found in other programming languages as
|
|
well. bf(Pascal) has its type tt(Boolean); bf(Java) has a tt(boolean)
|
|
type. Different from these languages, bf(C++)'s type tt(bool) acts like a kind
|
|
of tt(int) type. It is primarily a documentation-improving type, having just
|
|
two values tt(true) and tt(false). Actually, these values can be interpreted
|
|
as tt(enum) values for tt(1) and tt(0). Doing so would ignore the philosophy
|
|
behind the tt(bool) data type, but nevertheless: assigning tt(true) to an
|
|
tt(int) variable neither produces warnings nor errors.
|
|
|
|
Using the tt(bool)-type is usually clearer than using
|
|
tt(int). Consider the following prototypes:
|
|
verb(
|
|
bool exists(char const *fileName); // (1)
|
|
int exists(char const *fileName); // (2)
|
|
)
|
|
With the first prototype, readers expect the function to
|
|
return tt(true) if the given filename is the name of an existing
|
|
file. However, with the second prototype some ambiguity arises: intuitively
|
|
the return value 1 is appealing, as it allows constructions like
|
|
verb(
|
|
if (exists("myfile"))
|
|
cout << "myfile exists";
|
|
)
|
|
On the other hand, many system functions (like ti(access), ti(stat), and
|
|
many other) return 0 to indicate a successful operation, reserving other
|
|
values to indicate various types of errors.
|
|
|
|
As a rule of thumb I suggest the following: if a function should inform
|
|
its caller about the success or failure of its task, let the function return a
|
|
tt(bool) value. If the function should return success or various types of
|
|
errors, let the function return em(enum) values, documenting the situation by
|
|
its various symbolic constants. Only when the function returns a conceptually
|
|
meaningful integral value (like the sum of two tt(int) values), let the
|
|
function return an tt(int) value.
|