Problem with bool.....

Discussion in 'C++' started by Don, Nov 10, 2003.

  1. Don

    Don Guest

    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
    Don, Nov 10, 2003
    #1
    1. Advertising

  2. 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
    Andrey Tarasevich, Nov 10, 2003
    #2
    1. Advertising

  3. Don

    John Ericson Guest

    "Don" <> wrote in message
    news:bommg0$jqq$-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.
    John Ericson, Nov 10, 2003
    #3
  4. 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
    Andrey Tarasevich, Nov 10, 2003
    #4
  5. Don

    John Ericson Guest

    "Andrey Tarasevich" <> wrote in
    message news:...
    > 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
    John Ericson, Nov 10, 2003
    #5
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Weng Tianxiang
    Replies:
    2
    Views:
    461
    Weng Tianxiang
    Jun 21, 2005
  2. Chiller

    bool overload problem

    Chiller, Apr 11, 2004, in forum: C++
    Replies:
    2
    Views:
    302
    John Harrison
    Apr 11, 2004
  3. Sergei Gnezdov

    Different BOOL definitions problem

    Sergei Gnezdov, Mar 30, 2005, in forum: C Programming
    Replies:
    3
    Views:
    306
    Keith Thompson
    Mar 30, 2005
  4. k.sahici
    Replies:
    5
    Views:
    945
    dasjotre
    Jun 18, 2007
  5. small bool problem

    , Jan 17, 2008, in forum: C++
    Replies:
    2
    Views:
    460
Loading...

Share This Page