C++ use of bool
Tony van der Hoff
OSLib at mk-net.demon.co.uk
Fri May 5 09:48:13 BST 2000
On Thu, 4 May 2000, at 21:23:03, David J. Ruck <druck at freeuk.com> wrote
on the subject "C++ use of bool":
>I thought we had pretty much agreed to typedef our own variable to
>avoid having masses of compiler specific preprocessor statements to
>try and determine the compiler and its particular feature set -
>leaving you extreamly prone to problems with unexpected versions.
>
Indeed, and this is what I've done. Defmod now emits |osbool| instead of
|bool|, and that is the preferred usage. However, as you know, we are
committed to providing backwards compatibility wherever possible, and
for this reason we want to |typedef osbool bool| in cases where it
doesn't matter. Determining those cases is the present challenge.
Presently, the relevant part of types.h loks like this; I was hoping to
generalise matters:
------------------------------------------------------------
typedef unsigned int bits;
typedef int osbool;
typedef unsigned char byte;
/* for backward compatibility with non C++ and pre-C99 code
** we can define |bool| as osbool. Anyone who wants to provide
** their own definition of |bool| can define BOOL_DEFINED
*/
#if !defined BOOL_DEFINED && defined ( __STDC_VERSION__ )
/* this is necessary to avoid the "undefined macro" warning */
#if __STDC_VERSION__ >= 199901L
#define BOOL_DEFINED
#endif
#endif
/* C++ (1998) defines bool as a keyword */
/* CFront defines |__cplusplus| and |c_plusplus|, but not |bool| */
#if !defined( BOOL_DEFINED ) && defined __cplusplus && !defined
c_plusplus
#define BOOL_DEFINED
#endif
#if !defined( BOOL_DEFINED )
typedef osbool bool;
#endif
------------------------------------------------------------
If anyone has any better suggestions, I'd be pleased to hear them.
>I'd like to use BOOL as it is already in common use in libraries
>and user code, but for probably this reason osbool is better as
>it avoids the possibilty that BOOL isn't typedef'd to int.
>
Indeed; there were any number of options, but osbool seemed to have best
support. You can always |typedef osbool BOOL| if you so want in your own
code, and by defining |BOOL_DEFINED| you can ensure |bool| remains
undefined. I can't imagine there is not a solution here that will
satisfy everyone ;-)
--
Tony van der Hoff | Mailto:tony at mk-net.demon.co.uk
| Mailto:avanderhoff at iee.org
Buckinghamshire, England | http:www.mk-net.demon.co.uk
More information about the oslib-user
mailing list