Problem with bool.....

D

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
 
A

Andrey Tarasevich

Don said:
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).
 
J

John Ericson

Don said:
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.
 
A

Andrey Tarasevich

Andrey said:
...
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.
 
J

John Ericson

Andrey Tarasevich said:
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.

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
 

Ask a Question

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.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,767
Messages
2,569,570
Members
45,045
Latest member
DRCM

Latest Threads

Top