#if #ifdef #if defined The difference between several commonly used preprocessing instructions

When writing a shader, there are often some pre-compiled instructions such as #if #ifdef #if defined, etc. Sometimes it will be confused, so I decided to check the specific usage carefully and do a simple test.

#define

#define directive is used to define a macro or constant
#define has two common formats:

  1. #define identifier token-string Define a macro. All identifiers will be replaced with token-string during pre-compilation. You can replace a constant with a more understandable name, for example: #define PI 3.1415926
  2. #define identifier( argument0, ..., argumentN-1) token-string defines a function-like macro, for example: #define AREA(area, w, h) (area = w*h);

 

// #define 的使用 //
#define SOME_MACRO
#define PI 3.141593

The first method is used when defining macros. When the token-string is not specified, two points to note are:

  1. The identifier is still defined and can be detected using #if defined or #ifdef
  2. All identifier characters will be removed, or all the identifier characters will be replaced with empty strings.

For more information, please refer to the #define document
 

#if

#if is a preprocessing directive to control which part of the source file will be compiled.
The format is: #if condition is
simply the conditional statement (condition) following #if. If the execution result is not 0, the code in the #if statement block will be compiled, otherwise it will not be compiled.
#elif and #else can be compared to the regular keywords else if and else used to determine conditions. The difference is that a # symbol is added in front to indicate that the instruction is executed in the preprocessing stage, rather than at runtime. Finally, after all judgments are over, #endif needs to be used as the end to determine the scope of the prepared statement.

 

// #if 的使用 //
#define SOME_MACRO 0
#if SOME_MACRO
    return float4(1,1,1,1);
#else
    return float4(0,0,0,1);
#endif

The result of the above code returns black

 

// #if 的使用 //
#define SOME_MACRO
#if SOME_MACRO
    return float4(1,1,1,1);
#else
    return float4(0,0,0,1);
#endif

The above code will report an error: invalid or unsupported integer constant expression

For more information, please refer to #if document

 

#ifdef

#ifdef is used to determine whether a constant or macro is defined. The
format is: #ifdef identifier
identifier is a macro or constant, which can be defined by the #define directive. If the identifier is defined, the code in the #ifdef statement block will be compiled , Otherwise it will not be compiled.
#ifdef only judges whether a constant or macro is defined, it cannot be used for expression judgment, but #if can, for example:

 

#define CONST_VALUE 3
#if CONST_VALUE > 1
    return float4(1,1,1,1);
#else 
    return float4(0,0,0,1);
#endif

#ifdef CONST_VALUE > 1      // 这样写会报错 //

The statement in the hlsl document is that #ifdef is written only for compatibility with previous versions. It is recommended to use defined to judge, that is, use #if defined(MACRO) in this form, which will be discussed next.

 

These directives are provided only for compatibility with previous versions of the language. The use of the defined operator with the #if directive is preferred.

For more information, please refer to #ifdef document

 

#if defined

This is a more recommended way of writing in the document.
Both #if defined and #ifdef can be used to determine whether a macro is defined. #if !defined is equivalent to #ifndef.

 

#define SOME_MACRO

// #if defined 的使用 //
#if defined(SOME_MACRO)
    // do something //
#elif defined(OTHER_MACRO)
    // do something else //
#endif

// #ifdef 的使用 //
#ifdef SOME_MACRO
    // do something //
#endif

#ifdef OTHER_MACRO
    // do something else //
#endif

 

to sum up

  1. If it is based on whether a macro is defined to determine whether or not to execute a piece of code, it is recommended to use #if defined(MACRO)
  2. If it is based on whether the value of an expression is 0 to determine whether a piece of code should be executed, it is recommended to use the #if condition method
  3. #ifdef is used to judge whether a macro or constant is defined, not to judge an expression, #if can be used to judge an expression



Author: eight o'clock
link: https: //www.jianshu.com/p/5b763006b61e
Source: Jane books
are copyrighted by the author. For commercial reprints, please contact the author for authorization. For non-commercial reprints, please indicate the source.

Guess you like

Origin blog.csdn.net/u011105442/article/details/113108692