c++ - Why use preprocessor #if statements instead of if() else? -


i see being done time example in linux kernel. purpose of using preprocessor commands vs normal c++ if else block? there speed advantage or something?

a preprocessor changes c/c++ code before gets compiled (hence pre processor).

preprocessor ifs evaluated @ compile-time.

c/c++ ifs evaluated @ run-time.


you can things can't done @ run-time.

adjust code different platforms or different compilers:

#ifdef __unix__ /* __unix__ defined compilers targeting unix systems */ #include <unistd.h> #elif defined _win32 /* _win32 defined compilers targeting 32 or 64 bit windows systems */ #include <windows.h> #endif 

ensure header file definitions included once (equivalent of #pragma once, more portable):

#ifndef example_h #define example_h  class example { ... };  #endif 

you can make things faster @ run-time.

void some_debug_function() { #ifdef debug     printf("debug!\n"); #endif } 

now, when compiling debug not defined (likely command line parameter compiler), calls some_debug_function can optimized away compiler.


Comments

Popular posts from this blog

PHPMotion implementation - URL based videos (Hosted on separate location) -

javascript - Using Windows Media Player as video fallback for video tag -

c# - Unity IoC Lifetime per HttpRequest for UserStore -