![]() |
Defining a bool type
In another thread I saw someone included genlib.h, so looked it up and saw the following:
:
/*Would the above be more portable than what I have in some of my own code: :
#ifndef __cplusplusOr would this be even better: Quote:
:
#ifndef __cplusplusI could also define false like: #define false ('\0') I am not sure why that would be better though. I am using true != false, since true doesn't always have to be 1, but just not false. I do not care about other third-party libraries, so I just #undef them if they exist. I am also presuming that one only uses either C or C++, not another programming language like Objective C (of which I do not know if it has the bool type). |
The issues with booleans (defining the acceptable values, defining the name of the type, etc) are one of the reasons that C++ supports the bool type and true/false keywords.
In the examples above I would not bother with "#define true !false" or "enum bool {false, true};". Simpler forms that achieve exactly the same things are "#define true 1" and "enum bool {false, true};" respectively. The main problems with such techniques come in if you're reusing other people's code. There are many possible variants of this sort of scheme: using such techniques, no matter what you do, you will encounter some code that breaks because of mixing and matching two techniques. |
>Would the above be more portable than what I have in some of my own code
Not necessarily. You're assuming that true, false, and bool are all #define's. >enum bool{ false, true = !false }; There's no need to set the value for true, it'll be 1 since an unset starting value for enumerations is 0 and it increments by one for each constant defined. You'll also be forced to say "enum bool" whenever you want to use the type. A typedef is required to remove that if you want to use enumerations. >I am using true != false, since true doesn't always have to be 1, but just not false. It doesn't have to be 1 (with false being 0), but it sure does make your life easier when you follow that convention. Why? Because C has a tendency to assume it, such as when you use a boolean test: :
if ( done ) /* Equivalent to if ( done != 0 ):
typedef enum { jsw_false, jsw_true } jsw_bool; |
I think it's pretty much a personal, stylistic, issue so long as it works for you, and doesn't break anything. The fact is that, for a true boolean, a logical not and a bitwise not would result in the same thing. You don't have the degree of control necessary to force that behavior onto an integer.
|
i remember "inventing" this idea a while back. i thought i was so damn clever. then i saw it was common practice in C.
|
>then i saw it was common practice in C.
That's because the idea is so damn clever. :p Good ideas have a tendency to be discovered independently by a bunch of people. Just because an idea already exists doesn't diminish the accomplishment of you inventing it independently. |
Speaking of conflicts.
I work with a gigantic proprietary library. Keeping the vendor's name out of it - there are lots of conflicts with standard C and unix system calls. For example, some idiot created a round() function. If you want to use the round() in libm then you have to play games. I wrote re-wrote round as "PNMround()", so now we use that. PNM is a company name. This is NOT needful, you should never create situation like this. The same thing applies here with true and false. You simply need to be internally consistent with your libraries and the standard C library. You don't want the next guy - and there always is a next guy - to go nuts trying to work around conflicts that do not need to be there in the first place. |
| All times are GMT -5. The time now is 10:50 AM. |
Powered by vBulletin® Version 3.7.0, Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Copyright ©2007 DaniWeb® LLC