Can this macro be converted to a function?

后端 未结 16 1203
后悔当初
后悔当初 2020-12-05 16:27

While refactoring code and ridding myself of all those #defines that we\'re now taught to hate, I came across this beauty used to calculate the number of elements in a struc

16条回答
  •  情深已故
    2020-12-05 17:24

    As been stated, the code actually work out the number of elements in an array, not struct. I would just write out the sizeof() division explicitly when I want it. If I were to make it a function, I would want to make it clear in its definition that it's expecting an array.

    template
    inline size_t array_size(const T (&array)[SIZE])
    {
        return SIZE;
    }
    

    The above is similar to xtofl's, except it guards against passing a pointer to it (that says point to a dynamically allocated array) and getting the wrong answer by mistake.

    EDIT: Simplified as per JohnMcG. EDIT: inline.

    Unfortunately, the above does not provide a compile time answer (even if the compiler does inline & optimize it to be a constant under the hood), so cannot be used as a compile time constant expression. i.e. It cannot be used as size to declare a static array. Under C++0x, this problem go away if one replaces the keyword inline by constexpr (constexpr is inline implicitly).

    constexpr size_t array_size(const T (&array)[SIZE])
    

    jwfearn's solution work for compile time, but involve having a typedef which effectively "saved" the array size in the declaration of a new name. The array size is then worked out by initialising a constant via that new name. In such case, one may as well simply save the array size into a constant from the start.

    Martin York's posted solution also work under compile time, but involve using the non-standard typeof() operator. The work around to that is either wait for C++0x and use decltype (by which time one wouldn't actually need it for this problem as we'll have constexpr). Another alternative is to use Boost.Typeof, in which case we'll end up with

    #include 
    
    template
    struct ArraySize
    {
        private:    static T x;
        public:     enum { size = sizeof(T)/sizeof(*x)};
    };
    template
    struct ArraySize {};
    

    and is used by writing

    ArraySize::size
    

    where foo is the name of an array.

提交回复
热议问题