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

Eric Wing ewmailing at gmail.com
Mon Mar 18 16:48:34 PDT 2013


On 3/17/13, Jonathan Dearborn <grimfang4 at gmail.com> wrote:
> But to be portable to C++, you can't use C99 features.  Some common C++
> compilers do not provide C99.
>
> Jonny D

Is it really *some* compilers nowadays, or just Microsoft's
brain-dead, pathetic Visual Studio C compiler? (Yes, I know we won't
break compatibility with Visual Studio, but their sorry excuses are
really holding everybody back.)

> On 03/17/2013 07:28 AM, Vittorio Giovara wrote:
>> Is there any advantage in using bool at all?

In addition to some of the things already mentioned:
- C99 _Bool/bool guarantees that non-zero values are constrained to 1.
When you don't have this guarantee, you can get into trouble when
libraries don't sanitize the values they return (what happens if the
call returns 2 or 255 and you are explicitly comparing with == true).
You can also get into subtle bugs when you use masks and aren't paying
attention to this. (This was a long thread on the Apple Objective-C
mailing list not long ago.)

- Language bindings can benefit from an explicit bool. Languages like
Lua and Ruby treat everything except nil and false as true. This means
the number 0 is treated as true. For generating language bindings that
look at header files, explicit bools in the APIs express the actual
intent of the API and they can map to the real boolean types of the
target languages. Without other means of knowing the intent, the
binding must assume a number type which is distinctly different than a
boolean type for many languages. (Obj-C also adds encoding signatures
which can be detected at runtime, but the encoding signature is
different for _Bool and other types so it is the same problem as the
headers with respect to expressing intent.)

On 3/18/13, John <john at leafygreengames.com> wrote:
> Practically speaking, yes. You can freely mix C, C99, and C++ types. Many of
> us
> have been doing it for years across all the major platforms. It works. Don't
>
> hesitate to use all the standard C types. There is no portability issue with
> them.
>

Actually, I don't think that is completely true. I don't think most
C++ compilers handle/accept C99 _Complex. And not exactly a type, but
C++ officially doesn't handle restrict either and the cross-platform
compiler support is still shaky on that.

If you are using C99/C11, you can use _Bool or bool (stdbool.h). If
you are using C++, you can use bool. But in that case, remember you
are using C++, and not C. C++ is NOT a true superset of C and does
have subtle compatibility and portability implications. Most people
are not aware of these and don't necessarily hit them, but sadly I
seem to be bitten by these on a regular basis because I have to
fix/deal with other people's code who are not aware of these.

-Eric
--
Beginning iPhone Games Development
http://playcontrol.net/iphonegamebook/



More information about the SDL mailing list