Fitzwilliam Bennet-Darcy Fitzwilliam Bennet-Darcy - 3 years ago 144
C++ Question

Confusion around undefined behaviour when compiling for multi-platform

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___
#ifdef __GNUC__

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

Answer Source

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!

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download