Search code examples
c++gccstandards-compliance

How to make g++ refuse any code that exhibits undefined behaviour?


I would like to add a CXXFLAG to my build systems that force the entire code-base to be well-defined. So every piece of code that exhibits undefined behaviour in a static fashion, should be refused by the compiler.

For instance reinterpret_cast<A*>(someIntPtr)->aMember is without any runtime context undefined (a), while int i = bar(); i /= i; could result in undefined behaviour (b) depending on the runtime evaluation of bar() (which could return zero).
I only expect the (a) cases to be caught, not necessarily the (b) cases.


Solution

  • I'm not sure that your goal is computationally feasible.

    However, you'll get moderately close with -Wall -Wextra -Werror; look at the other warning options to see what else you want to enable.