Bruce Eckel's Thinking in C++, 2nd Ed | Contents | Prev | Next |
//: :arraySize.h // Uses template type induction to // discover the size of an array #ifndef ARRAYSIZE_H #define ARRAYSIZE_H template<typename T, int size> int asz(T (&)[size]) { return size; }
//: C19:ArraySize.cpp // The return value of the template function // asz() is a compile-time constant #include "../arraySize.h" int main() { int a[12], b[20]; const int sz1 = asz(a); const int sz2 = asz(b); int c[sz1], d[sz2];