By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
446,388 Members | 1,824 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 446,388 IT Pros & Developers. It's quick & easy.

Problem with bool.....

P: n/a
Don
Hi NG.
I am using some C code in my CPP code, like:
extern "C" {

#include "myC_Code.h"
}

CPP Code....
................
.............

My trouble is that some of my C function uses "bool" types. Therefore I
need, in the C files, to include "stdbool.h". But then my compiler reports
an error :-(. Strangely it reports a parsing error.......
Any ideas on how to resolve this issue?

Best Regards
Don
Jul 19 '05 #1
Share this Question
Share on Google+
4 Replies


P: n/a
Don wrote:
I am using some C code in my CPP code, like:
extern "C" {

#include "myC_Code.h"
}

CPP Code....
...............
............

My trouble is that some of my C function uses "bool" types. Therefore I
need, in the C files, to include "stdbool.h". But then my compiler reports
an error :-(. Strangely it reports a parsing error.......
Any ideas on how to resolve this issue?
...


Since in C99 language name 'bool' is a typedef-name for built-in type
'_Bool', your C++ compiler will complain when it encounters the 'typedef
_Bool bool' definition in 'stdbool.h'. 'bool' is a keyword in C++.

Maybe in your case the problem can be solved by selectively eliminating
the '#include <stdbool.h>' directive in C++ translation units. Instead
of including 'stdbool.h' unconditionally try doing this

#ifndef __cplusplus
#include <stdbool.h>
#endif /* __cplusplus */

However, I still think that using C99's type 'bool' in dual-language
header files is a bad idea. For example, a the following declaraion

void foo(bool);

when compiled as C99 code will be interpreted as

void foo(_Bool); /* C99's '_Bool' */

and when compiled as C++ it will be interpreted as

void foo(bool); /* C++'s 'bool' */

These are two different function signatures. Strictly speaking, I don't
know what in C++ and C99 standards guarantees that such C++ declaration
will be properly matched with corresponding C definition. As far as I
know, there's no such guarantee (correct me if I'm wrong).

--
Best regards,
Andrey Tarasevich

Jul 19 '05 #2

P: n/a
"Don" <no**@spam.dk> wrote in message
news:bo**********@news.net.uni-c.dk...
Hi NG.
I am using some C code in my CPP code, like:

extern "C" {

#include "myC_Code.h"

}

CPP Code....
...............
............

<snip>

Aside from Andry's point against muddling C++ built-in type
'bool' with C typedef, macro, or whatever 'bool' _Bool, why
are you putting extern "C" around your C code?? AFAIK that's
to suppress C++ name mangling (i.e. use C linkage
conventions).

John E.
Jul 19 '05 #3

P: n/a
Andrey Tarasevich wrote:
...
Since in C99 language name 'bool' is a typedef-name for built-in type
'_Bool', your C++ compiler will complain when it encounters the 'typedef
_Bool bool' definition in 'stdbool.h'. 'bool' is a keyword in C++.


I just noticed that 'bool' in C99 is not a typedef-name but a macro. In
this case my first point is not valid in its original form. However, a
different problem will arise. The macro definition inside 'stdbool.h'
will change the meaning of token 'bool' in your C++ translation unit,
causing all 'bool's to get replaced with '_Bool' . The C++ compiler will
choke on '_Bool' because it doesn't know what it is.

My second point still stands.

--
Best regards,
Andrey Tarasevich

Jul 19 '05 #4

P: n/a
"Andrey Tarasevich" <an**************@hotmail.com> wrote in
message news:vq************@news.supernews.com...
Andrey Tarasevich wrote:
...
Since in C99 language name 'bool' is a typedef-name for built-in type '_Bool', your C++ compiler will complain when it encounters the 'typedef _Bool bool' definition in 'stdbool.h'. 'bool' is a
keyword in C++.
I just noticed that 'bool' in C99 is not a typedef-name but a macro. In this case my first point is not valid in its original form. However, a different problem will arise. The macro definition inside 'stdbool.h' will change the meaning of token 'bool' in your C++ translation unit, causing all 'bool's to get replaced with '_Bool' . The C++ compiler will choke on '_Bool' because it doesn't know what it is.

My second point still stands.

--
Best regards,
Andrey Tarasevich


Your basic point's valid either way. ;) . I ran across
something similar recently (in C++ code, or at least the
code is compiled as C++ code), in which someone #defined
'true' and 'false' as 1 and 0, respectively. It just seems
so wrong to #define a keyword to something else, though
presumably when the code was written (in C?), 'true' and
'false' weren't keywords...

Best regards, JE
Jul 19 '05 #5

This discussion thread is closed

Replies have been disabled for this discussion.