Get the number of elements in an array

This is a C++ snippet, talking about array and sizeof

Get the number of elements in an array Add to Favorite

#define ITEMSOF(arr)    (sizeof(arr) / sizeof(0[arr]))

//C++ Version
template<int n>
struct char_array_wrapper{
    char result[n];
};

template<typename T, int s>
char_array_wrapper<s> the_type_of_the_variable_is_not_an_array(const T (&array)[s]){
}

#define ITEMSOF(v) sizeof(the_type_of_the_variable_is_not_an_array(v).result)

0[arr] is identical to arr[0] for arrays but will intentionally fail if it's used against a C++ object that overloads operator[] The C++ version is less intuitive but more type-safe

Created by ThePeppersStudio (142 days, 1.86 hours ago)

Do you want to leave a message? Please login first.