Equivalents to MSVC's _countof in other compilers?

后端 未结 4 1812
一向
一向 2020-12-03 04:54

Are there any builtin equivalents to _countof provided by other compilers, in particular GCC and Clang? Are there any non-macro forms?

相关标签:
4条回答
  • 2020-12-03 05:05

    Using C++11, the non-macro form is:

    char arrname[5];
    size_t count = std::extent< decltype( arrname ) >::value;
    

    And extent can be found in the type_traits header.

    Or if you want it to look a bit nicer, wrap it in this:

    template < typename T, size_t N >
    size_t countof( T ( & arr )[ N ] )
    {
        return std::extent< T[ N ] >::value;
    }
    

    And then it becomes:

    char arrname[5];
    size_t count = countof( arrname );
    
    char arrtwo[5][6];
    size_t count_fst_dim = countof( arrtwo );    // 5
    size_t count_snd_dim = countof( arrtwo[0] ); // 6
    

    Edit: I just noticed the "C" flag rather than "C++". So if you're here for C, please kindly ignore this post. Thanks.

    0 讨论(0)
  • 2020-12-03 05:13

    This?

    #define _countof(a) (sizeof(a)/sizeof(*(a)))

    0 讨论(0)
  • 2020-12-03 05:17

    I'm not aware of one for GCC, but Linux uses GCC's __builtin_types_compatible_p builtin to make their ARRAY_SIZE() macro safer (it'll cause a build break if applied to a pointer):

    /* &a[0] degrades to a pointer: a different type from an array */
    #define __must_be_array(a) \
     BUILD_BUG_ON_ZERO(__builtin_types_compatible_p(typeof(a), typeof(&a[0])))
    
    #define ARRAY_SIZE(arr) (sizeof(arr) / sizeof((arr)[0]) + __must_be_array(arr))
    

    Note: I think the BUILD_BUG_ON_ZERO() macro has a misleading name (it causes a build failure if the expression is not zero and returns 0 otherwise):

    /* Force a compilation error if condition is true, but also produce a
       result (of value 0 and type size_t), so the expression can be used
       e.g. in a structure initializer (or where-ever else comma expressions
       aren't permitted). */
    #define BUILD_BUG_ON_ZERO(e) (sizeof(struct { int:-!!(e); }))
    

    I think the naming for this macro comes from looking at it in two parts: BUILD_BUG_ON is what the macro does when the expression is true, and ZERO is the value 'returned' by the macro (if there's not a build break).

    0 讨论(0)
  • 2020-12-03 05:20

    Update: C++ 17 support std::size() (defined in header <iterator>)

    You can use boost::size() instead:

    #include <boost/range.hpp>
    
    int my_array[10];
    boost::size(my_array);
    
    0 讨论(0)
提交回复
热议问题