4

I've recently discovered that it's undefined behaviour to start anything with two underscores in C++.

So I've been fixing all my include guards.

But I've encountered code like

#ifdef __WINDOWS___
#endif
#ifdef __GNUC__
#endif

But isn't using __GNUC__ on a windows compiler UB and vice-versa? What should I do here?

Cœur
  • 37,241
  • 25
  • 195
  • 267
  • 3
    Double underscores are reserved for the implementation to define. These macros are defined (or not defined) by the implementation. – Barmar Jun 10 '16 at 07:11

2 Answers2

5

You misunderstand the rules slightly.

It's fine for you to use something that has (or hasn't) been #DEFINEd by your toolchain, even if it starts with two underscores.

It's certainly not fine for your to #DEFINE something starting with two underscores yourself.

This is a useful convention; it means that your source code cannot clash with the way your compiler implements the C++ standard.

And I'm glad you're fixing your include guards!

Bathsheba
  • 231,907
  • 34
  • 361
  • 483
1

First of all it would look better if you write:

#ifdef _WIN32
...
#endif

because _WIN32 is defined for all Windows C++/C compilers. Next Windows doesn't support GCC per se. It does have a minimalistic port called MinGW. And because of that you should write:

#ifdef __MINGW32__
...
#endif
rbaleksandar
  • 8,713
  • 7
  • 76
  • 161