Macro definition ARRAY_SIZE

前端 未结 5 2186
借酒劲吻你
借酒劲吻你 2021-02-19 10:55

I encountered the following macro definition when reading the globals.h in the Google V8 project.

// The expression ARRAY_SIZE(a) is a compile-time constant of t         


        
5条回答
  •  渐次进展
    2021-02-19 11:48

    In the Linux kernel, the macro is defined as (GCC specific):

    #define ARRAY_SIZE(arr) (sizeof(arr) / sizeof((arr)[0]) + __must_be_array(arr))
    

    where __must_be_array() is

    /* &a[0] degrades to a pointer: a different type from an array */
    #define __must_be_array(a) BUILD_BUG_ON_ZERO(__same_type((a), &(a)[0]))
    

    and __same_type() is

    #define __same_type(a, b) __builtin_types_compatible_p(typeof(a), typeof(b))
    

提交回复
热议问题