Jump to content
  • Advertisement
Sign in to follow this  
Basiror

C++: bool vs custom BOOL

This topic is 4684 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

Hi I am writing a few classes for my upcoming library and wondered about the bool type In the The C++ Standard Library by Nicolai M. Josuttis he states that and integral type will be converted to bool via automatic type conversion I use typedef int BOOL instead of bool *i am used to this since C day* now I wonder whether this is in the standard or not the MSDN doesn t mention the automatic type conversion in its description of bool so any ideas? If Josuttis is right BOOL operator ==(...); should be compatible with STL algorithms right?

Share this post


Link to post
Share on other sites
Advertisement
One reason to use an int, instead of a bool, is because the int can be hypothetically accessed faster, since it is a full 32-bits (assuming a 32-bit architecture).

Share this post


Link to post
Share on other sites
Quote:
Original post by Rattrap
To my knowledge, the standard says a bool is a 8-bit data type.


Which doesn't mean it won't be aligned on a 32 bit boundary. This is what really matters.

edit - I double-checked, the C++ standard says that sizeof(bool) is implementation-defined (§5.3.3-1 p77)

Share this post


Link to post
Share on other sites
Don't use BOOL -- use bool.
  1. Assigning a BOOL variable to a bool variable involves a conversion and it is not a simply copy.
  2. Comparing a BOOL variable to TRUE is risky and not guaranteed to give the expected result.
  3. BOOL is an obsolete hack. Why use it when bool is fully supported by the language?

Share this post


Link to post
Share on other sites
I don't think your actual question has been answered. Yes, the standard requires integral types to be converted to bool as neccessary. I am almost certain that the STL algorithms mearly require that your function's return value be convertable to bool, so returning an int should be acceptable.

That said, don't bother, as bool is the right way to go. Change your typedef to typedef bool BOOL; so that your existing code doesn't break.

CM

Share this post


Link to post
Share on other sites
Quote:
Original post by Conner McCloud
Change your typedef to typedef bool BOOL; so that your existing code doesn't break.


I'd recommend changing all your BOOL statements to bool with the replace command [wink] Less headaches down the line.

Share this post


Link to post
Share on other sites
In C++ you should always use the built-in bool (and true & false) if that's what you mean. It will actually change what functions are invoked on the data if there is an overload for int and bool. So the answer to "Will it work with the STL?" is maybe-yes, maybe-no.


Consider:

int(3) == int(4) -> false
bool(3) == bool(4) -> true

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

Participate in the game development conversation and more when you create an account on GameDev.net!

Sign me up!