S
Szabolcs Nagy
who has used _Bool? (or included stdbool.h for bool, true, false?)
(i'm considering using it in a library for home projects)
(i'm considering using it in a library for home projects)
Here's what Dennis Ritchie has to say about itSzabolcs Nagy said:who has used _Bool? (or included stdbool.h for bool, true, false?)
(i'm considering using it in a library for home projects)
Szabolcs said:who has used _Bool? (or included stdbool.h for bool, true, false?)
(i'm considering using it in a library for home projects)
IMHO, bool (or _Bool) goes hand in hand with static inline (and anSzabolcs said:who has used _Bool? (or included stdbool.h for bool, true, false?)
(i'm considering using it in a library for home projects)
Tor Rustad said:Since <stdbool.h> isn't available on all the relevant compilers, I
stil roll my own boolean definitions. I should perhaps switch to
lower-case now, using something ala:
#ifdef __STDC_IEC_559__
#include <stdbool.h>
#else
typedef enum {false, true} bool;
#endif
Ark Khasin said:IMHO, bool (or _Bool) goes hand in hand with static inline (and an
optimizing compiler). Otherwise, you will immediately pay performance
and code size penalties for syntactic sugar, and if you don't have to
care, C might not be the right language for your project.
I mean, types aside, semantic Booleans are not free:
int cmpneq(int a, int b) {return a!=b;}
is less efficient than
int cmpneq(int a, int b) {return a-b;}
Tor Rustad said:Since <stdbool.h> isn't available on all the relevant compilers, I
stil roll my own boolean definitions. I should perhaps switch to
lower-case now, using something ala:
#ifdef __STDC_IEC_559__
#include <stdbool.h>
#else
typedef enum {false, true} bool;
#endif
Tor said:Since <stdbool.h> isn't available on all the relevant compilers, I
stil roll my own boolean definitions. I should perhaps switch to
lower-case now, using something ala:
#ifdef __STDC_IEC_559__
#include <stdbool.h>
#else
typedef enum {false, true} bool;
#endif
Sorry. I ought to be more careful. Here is a repaired version:Keith said:The first has the considerable advantage of being correct. The
``a-b'' version exhibits undefined behavior on overflow. Consider
``cmpneq(INT_MIN, INT_MAX)''.
I may have limited exposure, but I've never seen an instruction set thatAnd how do you know that ``a!=b'' is less efficient than ``a-b''?
Have you measured it? Depending on the architecture, it's possible
that each could compile to a single instruction.
I may have limited exposure, but I've never seen an instruction set that
would invent a 1 or 0 out of non-equal or equal a and b in a single
instruction.
Every instruction set I've seen can invent a non-0 or a 0
out of non-equal or equal a and b in a single instruction [common
disclaimers on sizeof(int) vs. register size apply].
It's likely that a Boolean version can _never_ be more efficient than
the integer one, and in most cases is less efficient.
In the cases where any difference in efficiency would be noticed, theArk said:It's likely that a Boolean version can _never_ be more efficient than
the integer one, and in most cases is less efficient.
This is the same mistake asTor Rustad said:Since <stdbool.h> isn't available on all the relevant compilers, I stil
roll my own boolean definitions. I should perhaps switch to lower-case
now, using something ala:
#ifdef __STDC_IEC_559__
#include <stdbool.h>
#else
typedef enum {false, true} bool;
#endif
Szabolcs Nagy said:who has used _Bool? (or included stdbool.h for bool, true, false?)
(i'm considering using it in a library for home projects)
Ark Khasin said:Sorry. I ought to be more careful. Here is a repaired version:
int cmpneq(int a, int b) {return a^b;}
Well, #if true will do the wrong thing... But I can't think ofFor what it's worth, the standard says that true, false, and bool
are macros, not enumerations or typedef names. (Not that this
will often make a difference in practice.)
Ben said:For what it's worth, the standard says that true, false, and bool
are macros, not enumerations or typedef names. (Not that this
will often make a difference in practice.)
Eric Sosman said:Notice that the definition above is not quite equivalent
to C99's bool. Here's a way to see the difference:
bool b;
b = 42;
if (b == 1)
puts ("C99-style bool");
else
puts ("Trickery is rampant");
That is, converting *any* non-zero value to C99 bool produces
the value `(bool)1'.
Charlie said:Does it convert NaNs to true ?
[...]Ark Khasin said:Sorry. I ought to be more careful. Here is a repaired version:
int cmpneq(int a, int b) {return a^b;}
[...]cr88192 said:and, worse in the case of bool: it is not present...
the version of mingw I am using lacks stdbool.h...
and, what is more, so does cygwin...
Want to reply to this thread or ask your own question?
You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.