473,395 Members | 1,526 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,395 software developers and data experts.

Problem with bool.....

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
4 9331
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
"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
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
"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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

1
by: David Li | last post by:
I am having a lot of problem with following code. To start with I have a working sets of code and the top level SystemC code looks like this: ----------working main.cpp start here...
1
by: A. Saksena | last post by:
Hi all, I am trying to compile following code :- ======================================= #include <iostream.h> #include <string> namespace tlm { template <typename REQUEST, typename RESPONSE >...
6
by: Tony Johansson | last post by:
Hello Experts! I have the user defined class called Boolean that should be handle all kind of logic expression such as if (a && b && c) or if (!a && b && c) osv I have the class definition...
0
by: Lokkju | last post by:
I am pretty much lost here - I am trying to create a managed c++ wrapper for this dll, so that I can use it from c#/vb.net, however, it does not conform to any standard style of coding I have seen....
9
by: Tony | last post by:
I have an operator== overload that compares two items and returns a new class as the result of the comparison (instead of the normal bool) I then get an ambiguous operater compile error when I...
2
by: Constantine | last post by:
Hi, I have developed one class called CProductInfo providing == and != operator features. I have one problem. When I use this class in my program, say CProductInfo product = null; and...
3
by: Constantine | last post by:
Hi, I have developed one class called CProductInfo providing == and != operator features. I have one problem. When I use this class in my program, say CProductInfo product = null; and...
12
by: NOO Recursion | last post by:
Hi everyone! I am trying to write a program that will search a 12x12 for a thing called a "blob". A blob in the grid is made up of asterisks. A blob contains at least one asterisk. If an...
1
by: connor7777 | last post by:
Hi guys: We've been weeding out errors off of a java->c# project and have managed to redeem most of our code with the exception of one bug that we for some reason cannot pin down. The following...
12
by: whitehatmiracle | last post by:
Hello group I got the code of this classic elevator simulation. Im having some problem foloowing it. And i feel it can be simplified... can u people help me? here's the code: class button {...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.