Problem with bool.....



 DEVELOP > c-Plus-Plus > Problem with bool.....

LINK TO THIS PAGE  


rating :  0   |  0


  Page 1 of 1

1

 
Topic: DEVELOP > c-Plus-Plus
User: "Don"
Date: 09 Nov 2003 06:38:37 PM
Object: Problem with bool.....
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
.

User: "John Ericson"

Title: Re: Problem with bool..... 09 Nov 2003 07:24:02 PM
"Don" <nono@spam.dk> wrote in message
news:bommg0$jqq$1@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.
.

User: "Andrey Tarasevich"

Title: Re: Problem with bool..... 09 Nov 2003 07:06:18 PM
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
.
User: "Andrey Tarasevich"

Title: Re: Problem with bool..... 09 Nov 2003 07:45:30 PM
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
.
User: "John Ericson"

Title: Re: Problem with bool..... 09 Nov 2003 08:19:41 PM
"Andrey Tarasevich" <andreytarasevich@hotmail.com> wrote in
message news:vqtra4a97ccmeb@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
.




  Page 1 of 1

1

 


Related Articles
 

NEWER

pg.1232     pg.940     pg.716     pg.544     pg.412     pg.311     pg.234     pg.175     pg.130     pg.96     pg.70     pg.50     pg.35     pg.24     pg.16     pg.10     pg.6     pg.3     pg.1

OLDER