Is it possible for C preprocessor macros to contain preprocessor directives?

后端 未结 7 1285
醉话见心
醉话见心 2020-12-08 19:33

I would like to do the equivalent of the following:

#define print_max(TYPE) \\
#  ifdef TYPE##_MAX \\
     printf(\"%lld\\n\", TYPE##_MAX); \\
#  endif

prin         


        
相关标签:
7条回答
  • 2020-12-08 20:30

    As long as you're only interested in integral values, and assuming hardware using 2's compliment and 8-bit bytes:

    // Of course all this MAX/MIN stuff assumes 2's compilment, with 8-bit bytes...
    
    #define LARGEST_INTEGRAL_TYPE long long
    
    /* This will evaluate to TRUE for an unsigned type, and FALSE for a signed
     * type.  We use 'signed char' since it should be the smallest signed type
     * (which will sign-extend up to <type>'s size) vs. possibly overflowing if
     * going in the other direction (from a larger type to a smaller one).
     */
    #define ISUNSIGNED(type) (((type) ((signed char) -1)) > (type) 0)
    
    /* We must test for the "signed-ness" of <type> to determine how to calculate
     * the minimum/maximum value.
     *
     * e.g., If a typedef'ed type name is passed in that is actually an unsigned
     * type:
     *
     *  typedef unsigned int Oid;
     *  MAXIMUM_(Oid);
     */
    #define MINIMUM_(type)  ((type) (ISUNSIGNED(type) ? MINIMUM_UNSIGNED_(type)   \
                                  : MINIMUM_SIGNED_(  type)))
    
    #define MAXIMUM_(type)  ((type) (ISUNSIGNED(type) ? MAXIMUM_UNSIGNED_(type)   \
                              : MAXIMUM_SIGNED_(  type)))
    
    /* Minumum unsigned value; zero, by definition -- we really only have this
     * macro for symmetry.
     */
    #define MINIMUM_UNSIGNED_(type)     ((type) 0)
    
    // Maximum unsigned value; all 1's.
    #define MAXIMUM_UNSIGNED_(type)         \
         ((~((unsigned LARGEST_INTEGRAL_TYPE) 0))   \
          >> ((sizeof(LARGEST_INTEGRAL_TYPE) - sizeof(type)) * 8))
    
    /* Minimum signed value; a 1 in the most-significant bit.
     *
     * We use LARGEST_INTEGRAL_TYPE as our base type for the initial bit-shift
     * because we should never overflow (i.e., <type> should always be the same
     * size or smaller than LARGEST_INTEGRAL_TYPE).
     */
    #define MINIMUM_SIGNED_(type)       \
      ((type)               \
       ((signed LARGEST_INTEGRAL_TYPE)  \
        (~((unsigned LARGEST_INTEGRAL_TYPE) 0x0) << ((sizeof(type) * 8) - 1))))
    
    // Maximum signed value; 0 in most-significant bit; remaining bits all 1's.
    #define MAXIMUM_SIGNED_(type)       (~MINIMUM_SIGNED_(type))
    
    0 讨论(0)
提交回复
热议问题