17

In the source code of stdbool.h in LLVM project, it reads:

/* Don't define bool, true, and false in C++, except as a GNU extension. */
#ifndef __cplusplus
#define bool _Bool
#define true 1
#define false 0
#elif defined(__GNUC__) && !defined(__STRICT_ANSI__)
/* Define _Bool, bool, false, true as a GNU extension. */
#define _Bool bool
#define bool  bool
#define false false
#define true  true
#endif

In the last 4 lines there are three lines of the from #define X X. Why would you do that? What difference does it make? Wouldn't this force compiler to just replace, say, true with true?

Mohsen Nosratinia
  • 9,844
  • 1
  • 27
  • 52

3 Answers3

19

The only reason I can think of is, that preprocessor statements like

#ifdef bool
// do some stuff or define bool
#endif

in other c files include afterwards will work proper and not trying to redefine bool in another way like

#define bool int

which would interfere with the first definition

Mohsen Nosratinia
  • 9,844
  • 1
  • 27
  • 52
Martin Schlott
  • 4,369
  • 3
  • 25
  • 49
7
#define X X

has the effect that "the pre-processor conditional"*:

#ifdef X

is "true" "succeeds".*


* update

alk
  • 69,737
  • 10
  • 105
  • 255
  • 1
    Not me, but I would guess saying that a preprocessor directive is "true" got you the down vote. Some people are sticklers for accuracy. – john Sep 01 '13 at 12:20
  • @john: I was aware of this, that's why I put it in quotes. However it is a boolean expression in the context of pre-processing, isn't it? Do you have an idea for an alternative wording? – alk Sep 01 '13 at 12:25
  • Ok, I think the edit should fullfill anybodys expectations in accurate wording. – alk Sep 01 '13 at 12:30
  • And why would anyone chose it instead of just `#define X` ? – vsz Sep 01 '13 at 18:25
  • 3
    @vsz in this case it would redefine C++'s bool keyword to expand to nothing. – tab Sep 02 '13 at 04:53
2

It would make the difference that true, false etc are now macros. So code like this

#if defined(true)
    ...
#else
    ...
#endif

would be affected.

john
  • 85,011
  • 4
  • 57
  • 81