2

I was reading the GLFW library and I noticed they use GLFW_TRUE(1) and GLFW_FALSE(0). Now I've already seen that in other frameworks as well,making their own custom true/false identifiers. Is there any reason in creating your own custom true/false enums or classes in a framework? Does it make the code more readable for example? Is it a compatibility issue?

Another programmer speculated that they probably wouldn't want to #include <stdbool.h> but that header is in fact very small and wouldn't overcomplicate dependencies

laegirl
  • 144
  • 13

4 Answers4

3

I guess it is for backward compatibility reasons. The stdbool.h header was introduced in the C99 standard.

nza
  • 91
  • 5
2

Is there any reason in creating your own custom true/false enums or classes in a framework?

  • support of old or broken compilers (C99 incompatible, do not have stdbool.h and _Bool type)
  • static type checks with external tools (like cppcheck)
  • strong type checking by compiler (thinking of C++ classes with custom conversion)
  • subjective code readability

Does it make the code more readable for example?

Is subjective. For someone, not for someone else.

Is it a compatibility issue?

Most probably yes.


In case of GLFW it was written for windows in ~2002 (according to this site https://www.glfw.org/changelog ). My guess the windows compiler the author used didn't have stdbool.h. Either way, ask the author of the library.

KamilCuk
  • 120,984
  • 8
  • 59
  • 111
  • Wasn't the stdbool.h written in 98 though? Can you elaborate on the programmer missing the file? – laegirl Jan 06 '22 at 16:31
  • `Wasn't the stdbool.h written in 98 though?` There is one group of people, that create a "standard" that tells what should be in what standard header. There are other groups of people that implement those headers. There is time between a standard comes to existence and it being adopted by compiler writers. And Microsoft... does not care about any standards. `stdbool.h` was introduced in C standard in 1999, called C99 . `Can you elaborate on the programmer missing the file?` What is there to elaborate? MSVC featured C99 compatibility in 2013. – KamilCuk Jan 06 '22 at 16:41
0

I was reading the GLFW library and I noticed they use GLFW_TRUE(1) and GLFW_FALSE(0). Now I've already seen that in other frameworks as well,making their own custom true/false identifiers. Is there any reason in creating your own custom true/false enums or classes in a framework? Does it make the code more readable for example? Is it a compatibility issue?

If you plan to use your library in different environments, you can easily get to one that lacks those definitions in a header file. The earliest notable case of this, is when the curses library was developded (there was no <stdbool.h> yet, not the bool type defined in the standard library)

In those cases, it would be better to provide one definition yourself, but if you don't want to class with the standard ones, you have to define yours with some prefix that allows you to distinguish your definition from other libraries.

In C, bool is simply a subset of int, and true continues to mean != 0 while false is always 0. If you need to assing some value, some authors have inclined to use -1 as a value for true (which is not normally the case, 1 and -1 are obviously different)

Luis Colorado
  • 10,974
  • 1
  • 16
  • 31
-2

Probably for readability issues and controlling the flow of the framework.

SuperStormer
  • 4,997
  • 5
  • 25
  • 35