UNIX Socket FAQ

A forum for questions and answers about network programming on Linux and all other Unix-like systems

You are not logged in.

  • Index
  • » C++
  • » C99 'bool' type == C++ 'bool' type?

#1 2010-04-28 04:07 PM

tomcio
Member
From: Poland
Registered: 2006-08-14
Posts: 113

Re: C99 'bool' type == C++ 'bool' type?

C99 introduced new type 'bool'. Is it compatible with C++'s 'bool' type?

Offline

#2 2010-04-28 08:39 PM

RobSeace
Administrator
From: Boston, MA
Registered: 2002-06-12
Posts: 3,829
Website

Re: C99 'bool' type == C++ 'bool' type?

Compatible in what manner?  You mean storage size?  Like if you wrote out a "bool"
to a file (or sent it over a socket) in a C app, and then read it into a "bool" in a C++
app, would it work?  I suspect it probably is compiler dependent, and you'd be wise
to not assume they're identical...  Best to just use a simple "char" (or "int8_t") for
cross-platform interchange like that...  However, I wouldn't be surprised if most/all
systems/compilers treat them both identically...  It'd be rather pointless to implement
"bool" any alternate way...  (Unless maybe you had a bunch together in a single
location, and then maybe a smart compiler might turn them into bit fields...  Eg: to save
space in a struct...)

Offline

#3 2010-04-28 09:30 PM

tomcio
Member
From: Poland
Registered: 2006-08-14
Posts: 113

Re: C99 'bool' type == C++ 'bool' type?

I was thinking about such situation:

I create a library in C, where I expose function:

bool some_function (bool some_arg);

Later I would like to use this library in C++ program. Should it work?

Offline

#4 2010-04-28 10:20 PM

RobSeace
Administrator
From: Boston, MA
Registered: 2002-06-12
Posts: 3,829
Website

Re: C99 'bool' type == C++ 'bool' type?

I suspect it depends on your system and compiler...  I believe it will work on a glibc
system with GCC/G++...  Take a look at your local <stdbool.h>...  In mine, it does this:

#ifndef __cplusplus

#define bool    _Bool
#define true    1
#define false   0

#else /* __cplusplus */

/* Supporting <stdbool.h> in C++ is a GCC extension.  */
#define _Bool   bool
#define bool    bool
#define false   false
#define true    true

#endif /* __cplusplus */

So, it seems this is a GCC/glibc trick, and may not necessarily work on other
systems/compilers...

Also, since the real C99 type is "_Bool" with "bool" just being a macro cover for it,
a C++ compiler probably won't know anything about it without trickery like the
above...

So, in short, unless you know your target system/compiler supports it, you'd probably
be better off avoiding it and using a simple "char" or "int" or something instead...
C++ still supports C-style handling by converting any non-zero int value into "true"
and zero into "false" for its bools, and doing the inverse conversion of them back
to ints as needed, so there'd be nothing stopping you from treating a function that
returned or took as an arg "char" or "int" from being assigned to a C++ "bool" or
passing one to it as an arg...

Offline

#5 2010-04-28 11:34 PM

i3839
Oddministrator
From: Amsterdam
Registered: 2003-06-07
Posts: 2,230

Re: C99 'bool' type == C++ 'bool' type?

Personally I don't see the point of using bool types anyway. Just use ints.
Bools don't add type safety anyway.

Offline

#6 2010-04-29 12:45 PM

RobSeace
Administrator
From: Boston, MA
Registered: 2002-06-12
Posts: 3,829
Website

Re: C99 'bool' type == C++ 'bool' type?

About the only feature real (C99 or C++) bools give you is turning all non-zero values
into the exact value 1...  But, honestly, I'm not sure how useful such a feature really
is...  Personally, that feature has annoyed the hell out of me on more than one occassion
before...  (Eg: wanting to add a third condition to an existing "bool"-based API, without
needing to break backward compatability...  Such that you can pass or it can return
some value other than 0/false or 1/true to get some alternate behavior...  With a standard
int or char, it's trivial of course; either just use 2 or -1 or whatever...  People unaware
of this behavior will still treat the value as true, for all practical purposes, but those
who are aware can look for that specific value and behave differently from the 1/true
case, if they choose...  Yes, technically speaking this is a kluge, and what you're
dealing with then is no longer a boolean value...  But, still, such kluges are sometimes
still useful...  A good example is a verbosity or debug level: every time you add "-v"
or "-d" to the command-line, it increments the level...  Most places can just test it
like a bool to see if it's non-zero, while specific places can test for specific values
and only output stuff if the value is high enough...)

Offline

  • Index
  • » C++
  • » C99 'bool' type == C++ 'bool' type?

Board footer

Powered by FluxBB