Interested in how the compiler will behave in the following cases (it will remove the condition or not and at what level of optimization is included)
1.1)
if (true) { // какой-то код } 1.2)
if (true) { // какой-то код } else { // какой-то код } 2.1)
const bool isEnable = true; if (isEnable) { // какой-то код } 2.2)
const bool isEnable = true; if (isEnable) { // какой-то код } else { // какой-то код } 3.1)
constexpr bool isEnable = true; if (isEnable) { // какой-то код } 3.2)
constexpr bool isEnable = true; if (isEnable) { // какой-то код } else { // какой-то код } It is also interesting what are the ways to create #IFDEF , which will enable / disable pieces of code depending on true or false , specified at the compilation stage.
C and C++ . But on С can hardly think of something like that, but on the plus points of the new standards you can (up to C++14 inclusive)
UPD. By on / off is meant that
if (true) { // какой-то код } will turn out
// какой-то код BUT
if (false) { // какой-то код } It will be thrown out by the compiler absolutely
if- onlyif constexpr. As for the exclusion of "extra" code after verification, then any self-respecting compiler will do it. There is no general concept of "optimization level". - AnTifalways true and therefore will leave only the true branch from it already on compilation. This probably happens with flags of type-O3, but if this happens without optimization, I don’t know - Andrei Kurulev#define ON 1and then#if ON ... #endif? - AnT