explanation.cxx (500B)
1 #include <iostream> 2 3 const int ARR_SIZE = 4; 4 5 template<typename T> 6 int GetArrayLastItemIndex(T a[]) { 7 int k = 0; 8 9 for (int i = 0; i < ARR_SIZE and a[i] != NULL; i++) k++; 10 11 return k; 12 } 13 14 15 int main() 16 { 17 int a[ARR_SIZE] = { NULL }; 18 char b[ARR_SIZE] = { '$', '4', 'NULL', NULL }; 19 double c[ARR_SIZE] = { 2.3, NULL }; 20 21 std::cout << GetArrayLastItemIndex(a) << std::endl; // 0 22 std::cout << GetArrayLastItemIndex(b) << std::endl; // 3 23 std::cout << GetArrayLastItemIndex(c) << std::endl; // 1 24 }