Advantages of a Conditional Preprocessor over Conditional Statements

I have never worked with #if , #ifdef , #ifndef , #else , #elif and #endif .

When I looked through some source codes, I found widespread use of these directives. Some reading on conditional preprocessors, but did not find a concept like how they differ from ordinary conditional operators . So I was wondering what is the advantage of the following code:

#include<iostream> int main() { int i = 0; #if i == 0 std::cout<<"This"; #else std::cout<<"That"; #endif return 0; } 

above this:

 #include<iostream> int main() { int i = 0; if (i == 0) std::cout<<"This"; else std::cout<<"That"; return 0; } 

Also, if you use / don't use a conditional preprocessor?

+6
source share
5 answers

The conditional preprocessor does not work, as in the first example.

It works with constants, understand? During compilation, it considers various conditions and places / excludes the source code according to it.

For instance:

 #define HAS_COMPARISON int main() { #ifdef HAS_COMPARISON int i = 0; if(i == 0) std::cout << "This"; else #else std::cout << "That"; #endif } 

Using the define set, it will set the i variable and perform a comparison ... In short, it will output This . If you comment on this definition, the whole block will not be in your program, which means that it will always output That , without setting a variable or without performing a comparison.

This is the most common use of the preprocessor. You can also define values ​​and compare them to have the behavior of a variable with the same definition, but this is another problem.

Once again: the conditional preprocessor is evaluated at compile time, condition variables are evaluated at runtime.

+3
source

The example shown by you does not seem useful due to the lack of other information. But here is an example that #if is useful.

 #if OS == LINUX //do something #elif OS == SOLARIS //do something else #else // #endif 

The key is that #if is evaluated at compile time, but if is evaluated at program startup.

 #if BYTE_ORDER == LITTLE_ENDIAN //do something #else //do something else #endif 
+4
source

Using preprocessor directives in this case is not entirely useful. But using these preprocessor directives is useful in many other cases.

These preprocessor directives can be used for conditional compilation. for example, if a program needs to be designed for multiple platforms, then platform-specific values ​​may be assigned values. Changing these compilations of platform-specific values ​​can be done while all the code can be supported as one large object.

It is also useful when debugging. Tested modules can be compiled into code and executed using these conditional compilations during debugging, and they can be stopped from compilation using this data.

+2
source

Conditional compilation means that ifdef-ed code is never in the last associated application. Just using conditional language expressions means both branches are in the final code, which makes it big and potentially harder to test, etc.

Use #ifdef etc. when at compile time you know what is required. Language conditions are used when you do not know what you need before execution.

+1
source

The advantage of the preprocessor is that the code is thrown away. It does not compile (takes time), and it does not generate machine code that will be loaded into ram. If the solution is in a VERY tight loop performed LOTS times, there may be an improvement in speed. Do not think that this is important if you are not really.

Preprocessor corruption is something that you obviously need to know the answer at compile time. The source code now contains a lot of code that can never be executed. It’s harder to follow a person because it’s often difficult to determine what these compilation times would be.

+1
source

Source: https://habr.com/ru/post/946883/


All Articles