Conditional operator with a constant (true) value?

后端 未结 2 1397
醉话见心
醉话见心 2021-02-02 09:28

I was looking at some preprocessor macros used in OpenSSL, and I came across the following from crypto/stack/safestack.h:

#define CHECKED_STACK_OF(t         


        
2条回答
  •  囚心锁ツ
    2021-02-02 10:16

    It's code that double checks if the correct type is passed. The pointer p is passed and along the type of that pointer must be also manually typed in the macro.

    The ternary expression will always return the second operand but both second and third operands will be checked if their type matches, and if they don't you should get a compiler error.

    A simple example:

    int* p = NULL ;
    
    1 ? p : ( float* )p ;    //error
    
    1 ? p : ( int* )p ;      //ok
    

提交回复
热议问题