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

  • one
    What exactly do you mean by "does he remove the condition or not"? It is impossible to make the compiler not see at all and check "some code" via if - only if 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". - AnT
  • Well, for example, he will see that if always 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
  • It is difficult to answer this question, for it is not clear what purpose it pursues and what it matters. Leave / not leave - what's the difference. And you will see that it leaves - you will raise the level of optimization. - AnT
  • one
    It is still not clear what you mean by "on-off". Make it so that the compiler does not see at all and does not check the code turned off? And what's the problem with the usual #define ON 1 and then #if ON ... #endif ? - AnT
  • 2
    @AndreyKurulev: In general, the compiler is limited only by the as if rule. That is, he has the right to throw out anything, just to display the same thing on the screen and in the files. And add anything. And also has the right and not to throw out and not to add. There are no "obligations" for optimization at the compiler. - VladD

1 answer 1

In C ++ 17 there will be if constexpr :

 constexpr auto cond = ...; if constexpr (cond) { // код return 200; } else if (expr) { // код return "100"; } else { // код return false; } 

For now, there is nothing like that, only std::enable_if .

  • Again, if the compile-time expression is a constant, then the compiler is optimized without if constexpr . The latter is more likely to exclude pieces that simply cannot be compiled and is usually used in generic code. So in this question it is superfluous, a normal compiler will suffice if - int3