Thread: BOOL instead of bool?

  1. #1
    Registered User
    Join Date
    Nov 2009
    Posts
    151

    BOOL instead of bool?

    How come in the Win32 API, you're supposed to use BOOL instead of bool, TRUE instead of true, etc. It just seems unnecessary to me.

  2. #2
    Guest Sebastiani's Avatar
    Join Date
    Aug 2001
    Location
    Waterloo, Texas
    Posts
    5,708
    Because the API is written in C.
    Code:
    #include <cmath>
    #include <complex>
    bool euler_flip(bool value)
    {
        return std::pow
        (
            std::complex<float>(std::exp(1.0)), 
            std::complex<float>(0, 1) 
            * std::complex<float>(std::atan(1.0)
            *(1 << (value + 2)))
        ).real() < 0;
    }

  3. #3
    Registered User
    Join Date
    Nov 2009
    Posts
    151
    OK, that makes sense

  4. #4
    Master Apprentice phantomotap's Avatar
    Join Date
    Jan 2008
    Posts
    5,108
    It will make substantially less sense when you find that most functions returning a `BOOL' actually use a three value system.

    Soma

  5. #5
    Registered User
    Join Date
    Aug 2010
    Location
    Poland
    Posts
    733
    I would say it IS necessary. Whenever you use a library, you should stick to its rules and typedefs as they might change in the future.
    Actually, WinAPI's BOOL is defined as int (in my implementation), so it's not the same as bool. Whenever I write WinAPI wrappers I use its typedefs, even those for INTs (some WinAPI's parameters are UINTs some DWORDs those can be different in practise).

    Also, when a WinAPI's function returns BOOL, you shall not compare it with TRUE when the documentation says "function returns 0 on success, otherwise non-zero"
    Last edited by kmdv; 08-25-2010 at 04:20 AM.

  6. #6
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    I think the point is that it uses BOOL instead of bool because the Windows API was written for C90, and C90 doesn't have the bool type.
    Therefore, the creators of the API had to invent their own boolean type, which was named BOOL. That's all there is to it.
    It was a discussion of whether it was fine using bool instead of BOOL when dealing with these functions, I believe. I could be wrong, though.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  7. #7
    Registered User
    Join Date
    Aug 2010
    Location
    Poland
    Posts
    733
    Note that there might be also a structure containing a BOOL field, and a WORD following. Data alignment issues must be taken into consideration as well.

  8. #8
    Guest Sebastiani's Avatar
    Join Date
    Aug 2001
    Location
    Waterloo, Texas
    Posts
    5,708
    Quote Originally Posted by phantomotap View Post
    It will make substantially less sense when you find that most functions returning a `BOOL' actually use a three value system.

    Soma
    That has tripped up quite a few, yes (the "GetMessage catastrophe", to wit). Anyway, the documentation will (should, anyway) say so, if it does.

    Note that there might be also a structure containing a BOOL field, and a WORD following. Data alignment issues must be taken into consideration as well.
    Why so?
    Code:
    #include <cmath>
    #include <complex>
    bool euler_flip(bool value)
    {
        return std::pow
        (
            std::complex<float>(std::exp(1.0)), 
            std::complex<float>(0, 1) 
            * std::complex<float>(std::atan(1.0)
            *(1 << (value + 2)))
        ).real() < 0;
    }

  9. #9
    Registered User
    Join Date
    Oct 2006
    Posts
    250
    Quote Originally Posted by Elysia View Post
    I think the point is that it uses BOOL instead of bool because the Windows API was written for C90, and C90 doesn't have the bool type.
    Therefore, the creators of the API had to invent their own boolean type, which was named BOOL. That's all there is to it.
    I beg to differ. The entire Win32 API is built around typedefs, so even if there were a native bool type, it would probably have been wrapped up in a neat typedef.
    iMalc: Your compiler doesn't accept misspellings and bad syntax, so why should we?
    justin777: I have no idea what you are talking about sorry, I use a laptop and there is no ascii eject or something

  10. #10
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    That is possible. The fact that BOOL is really int since there's no native bool type still remains, though.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  11. #11
    Registered User
    Join Date
    Aug 2010
    Location
    Poland
    Posts
    733
    Note that there might be also a structure containing a BOOL field, and a WORD following. Data alignment issues must be taken into consideration as well.
    Why so?
    So the reason of typedefing BOOL was not only that there was no native bool type back then. There is also INT, FLOAT etc.

    That is possible. The fact that BOOL is really int since there's no native bool type still remains, though.
    Why not typedefed as BYTE...
    Last edited by kmdv; 08-25-2010 at 12:22 PM.

  12. #12
    Guest Sebastiani's Avatar
    Join Date
    Aug 2001
    Location
    Waterloo, Texas
    Posts
    5,708
    Quote Originally Posted by kmdv View Post
    So the reason of typedefing BOOL was not only that there was no native bool type back then. There is also INT, FLOAT etc.
    That doesn't answer my question: what has it got to do with alignment?

    Quote Originally Posted by kmdv View Post
    Why not typedefed as BYTE...
    Because C implicitly promotes stack arguments smaller than int to an int, maybe?
    Code:
    #include <cmath>
    #include <complex>
    bool euler_flip(bool value)
    {
        return std::pow
        (
            std::complex<float>(std::exp(1.0)), 
            std::complex<float>(0, 1) 
            * std::complex<float>(std::atan(1.0)
            *(1 << (value + 2)))
        ).real() < 0;
    }

  13. #13
    Registered User
    Join Date
    Aug 2010
    Location
    Poland
    Posts
    733
    That doesn't answer my question: what has it got to do with alignment?
    BOOL seemed unnecessary to one of the posters. I said it might have been typedefed for such purposes also.

    Because C implicitly promotes stack arguments smaller than int to an int, maybe?
    Maybe, but this type might be used in structs too (might, I don't know if there is even one struct that does)



    So.. it's all I had to say

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. USB1: Undefined reference to...
    By Oaktree in forum C Programming
    Replies: 6
    Last Post: 01-21-2010, 11:33 AM
  2. Code review
    By Elysia in forum C++ Programming
    Replies: 71
    Last Post: 05-13-2008, 09:42 PM
  3. Smart pointer class
    By Elysia in forum C++ Programming
    Replies: 63
    Last Post: 11-03-2007, 07:05 AM
  4. Headers that use each other
    By nickname_changed in forum C++ Programming
    Replies: 7
    Last Post: 10-03-2003, 04:25 AM
  5. Need Help With an RPG Style Game
    By JayDog in forum Game Programming
    Replies: 6
    Last Post: 03-30-2003, 08:43 PM