Can this macro be converted to a function?

后端 未结 16 1221
后悔当初
后悔当初 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:09

    Simplfying @KTC's, since we have the size of the array in the template argument:

    template
    int arraySize(const T(&arr)[SIZE])
    {
        return SIZE;
    }
    

    Disadvantage is you will have a copy of this in your binary for every Typename, Size combination.

提交回复
热议问题