[SDL] C++ / SDL using bool vs. SDL_bool

John john at leafygreengames.com
Sun Mar 17 09:38:38 PDT 2013


It's just a matter of it being a modern built-in type. You would use it for the 
same reasons you would use the built-in "string" type in a language. The 
advantage is code clarity and widespread familiarity.

In C, there is a small technical advantage to using the built-in bool over a 
UDT. bool might minimize use of space in memory, registers, etc., because bool 
is typically 8-bits, while enums are typically native ints, unless forced to be 
otherwise via compiler-specific options.

In C++, there are several technical advantages of bool being a distinct type, 
such as overloading, template specialization and implicit conversion.

For SDL, I'd say the advantage is mostly one of keeping up appearances. :)



On 03/17/2013 07:28 AM, Vittorio Giovara wrote:
> Is there any advantage in using bool at all?
> As far as I know packing one bit in memory or in a struct will usually
> wrap the variable or struct in at least a byte segment in memory; so
> using a uint8_t type and a 0 or 1 value could achieve the same thing
> and without adding a new type.
> Or did I miss something?
> Vittorio
>
> Sent from my iPad Mini
>
> On 17/mar/2013, at 00:45, John <john at leafygreengames.com> wrote:
>
>> C99 declares both bool and _Bool, plus a macro, __bool_true_false_are_defined.
>> The point of the macro is so you can fix these sort of custom boolean types in older code without breaking anything.
>>
>>
>>
>> On 03/16/2013 05:47 PM, Sik the hedgehog wrote:
>>> I think C99's support for boolean values consists of stdbool.h and a
>>> _Bool type (*not* bool). Correct me if I'm wrong.
>>>
>>> 2013/3/16, Alex Barry <alex.barry at gmail.com>:
>>>> On Sat, Mar 16, 2013 at 4:52 PM, Steven Noonan
>>>> <steven at uplinklabs.net>wrote:
>>>>
>>>>> The 'bool' type was added in C99 (see section 7.16 in the C99 standard).
>>>> That's true, but I think Sam's logic is to support the lowest common
>>>> denominator - in this case, not every compiler will support a bool in
>>>> standard C, and it is typical for a C library to define a TRUE/FALSE value.
>>>>
>>>> I think typically, if a programmer is using SDL from C++, they would be
>>>> wrapping a lot of functions in C classes where SDL_Bool could be translated
>>>> into bool true/false values.
>>> _______________________________________________
>>> SDL mailing list
>>> SDL at lists.libsdl.org
>>> http://lists.libsdl.org/listinfo.cgi/sdl-libsdl.org
>> _______________________________________________
>> SDL mailing list
>> SDL at lists.libsdl.org
>> http://lists.libsdl.org/listinfo.cgi/sdl-libsdl.org
> _______________________________________________
> SDL mailing list
> SDL at lists.libsdl.org
> http://lists.libsdl.org/listinfo.cgi/sdl-libsdl.org
>



More information about the SDL mailing list