exp2l: Work around a NetBSD 10.0/i386 bug.
[gnulib.git] / lib / stdbool.in.h
blobe80ef59f2a2596f6b54863cb8cccdbffab8ab85b
1 /* Copyright (C) 2001-2003, 2006-2024 Free Software Foundation, Inc.
2 Written by Bruno Haible <haible@clisp.cons.org>, 2001.
4 This file is free software: you can redistribute it and/or modify
5 it under the terms of the GNU Lesser General Public License as
6 published by the Free Software Foundation; either version 2.1 of the
7 License, or (at your option) any later version.
9 This file is distributed in the hope that it will be useful,
10 but WITHOUT ANY WARRANTY; without even the implied warranty of
11 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 GNU Lesser General Public License for more details.
14 You should have received a copy of the GNU Lesser General Public License
15 along with this program. If not, see <https://www.gnu.org/licenses/>. */
17 #ifndef _GL_STDBOOL_H
18 #define _GL_STDBOOL_H
20 /* ISO C 99 <stdbool.h> for platforms that lack it. */
22 /* Usage suggestions:
24 Programs that use <stdbool.h> should be aware of some limitations
25 and standards compliance issues.
27 Standards compliance:
29 - <stdbool.h> must be #included before 'bool', 'false', 'true'
30 can be used.
32 - You cannot assume that sizeof (bool) == 1.
34 - Programs should not undefine the macros bool, true, and false,
35 as C99 lists that as an "obsolescent feature".
37 Limitations of this substitute, when used in a C89 environment:
39 - <stdbool.h> must be #included before the '_Bool' type can be used.
41 - You cannot assume that _Bool is a typedef; it might be a macro.
43 - Bit-fields of type 'bool' are not supported. Portable code
44 should use 'unsigned int foo : 1;' rather than 'bool foo : 1;'.
46 - In C99, casts and automatic conversions to '_Bool' or 'bool' are
47 performed in such a way that every nonzero value gets converted
48 to 'true', and zero gets converted to 'false'. This doesn't work
49 with this substitute. With this substitute, only the values 0 and 1
50 give the expected result when converted to _Bool' or 'bool'.
52 - C99 allows the use of (_Bool)0.0 in constant expressions, but
53 this substitute cannot always provide this property.
55 Also, it is suggested that programs use 'bool' rather than '_Bool';
56 this isn't required, but 'bool' is more common. */
59 /* 7.16. Boolean type and values */
61 #ifdef __cplusplus
62 # if !defined _MSC_VER
63 # define _Bool bool
64 # define bool bool
65 # endif
66 #else
67 # if !defined __GNUC__
68 /* If @HAVE__BOOL@:
69 Some HP-UX cc and AIX IBM C compiler versions have compiler bugs when
70 the built-in _Bool type is used. See
71 https://gcc.gnu.org/ml/gcc-patches/2003-12/msg02303.html
72 https://lists.gnu.org/r/bug-coreutils/2005-11/msg00161.html
73 https://lists.gnu.org/r/bug-coreutils/2005-10/msg00086.html
74 Similar bugs are likely with other compilers as well; this file
75 wouldn't be used if <stdbool.h> was working.
76 So we override the _Bool type.
77 If !@HAVE__BOOL@:
78 Need to define _Bool ourselves. As 'signed char' or as an enum type?
79 Use of a typedef, with SunPRO C, leads to a stupid
80 "warning: _Bool is a keyword in ISO C99".
81 Use of an enum type, with IRIX cc, leads to a stupid
82 "warning(1185): enumerated type mixed with another type".
83 Even the existence of an enum type, without a typedef,
84 "Invalid enumerator. (badenum)" with HP-UX cc on Tru64.
85 The only benefit of the enum, debuggability, is not important
86 with these compilers. So use 'signed char' and no enum. */
87 # define _Bool signed char
88 # else
89 /* With this compiler, trust the _Bool type if the compiler has it. */
90 # if !@HAVE__BOOL@
91 /* For the sake of symbolic names in gdb, define true and false as
92 enum constants, not only as macros.
93 It is tempting to write
94 typedef enum { false = 0, true = 1 } _Bool;
95 so that gdb prints values of type 'bool' symbolically. But then
96 values of type '_Bool' might promote to 'int' or 'unsigned int'
97 (see ISO C 99 6.7.2.2.(4)); however, '_Bool' must promote to 'int'
98 (see ISO C 99 6.3.1.1.(2)). So add a negative value to the
99 enum; this ensures that '_Bool' promotes to 'int'. */
100 typedef enum { _Bool_must_promote_to_int = -1, false = 0, true = 1 } _Bool;
101 # endif
102 # endif
103 # define bool _Bool
104 #endif
106 /* The other macros must be usable in preprocessor directives. */
107 #ifdef __cplusplus
108 # if !defined _MSC_VER
109 # define false false
110 # define true true
111 # endif
112 /* In Sun C++ 5.11 (Solaris Studio 12.2) and older, 'true' as a preprocessor
113 expression evaluates to 0, not 1. Fix this by overriding 'true'. Note that
114 the replacement has to be of type 'bool'. */
115 # if defined __SUNPRO_CC && true != 1
116 # undef true
117 # define true (!false)
118 # endif
119 #else
120 # define false 0
121 # define true 1
122 #endif
124 #define __bool_true_false_are_defined 1
126 #endif /* _GL_STDBOOL_H */