I've tried:
template <typename T,unsigned S>
unsigned getArraySize(const T (&v)[S]) { return S; }
after Motti's answer https://mcmap.net/q/63572/-how-do-i-find-the-length-of-an-array
but I've got this message:
error C2265: '' : reference to a zero-sized array is illegal
What's wrong with my compiler?
I gave a look at this page: http://social.msdn.microsoft.com/Forums/windowsdesktop/en-US/4b78bcef-4c33-42f1-a4c5-fb6f702ced0b/vs6-c-compile-error-using-getaddrinfo so I tried this solution:
template <typename T,unsigned S>
unsigned getArraySize(const T v[S]) { return S; }
this compiles, but when I try to use it:
double myX[2] = {7,3};
std::cout << getArraySize(myX) << std::endl;
I get a compilation error:
error C2783: 'unsigned int __cdecl getArraySize(const T [])' : could not deduce template argument for 'S'
Beside changing the compiler, is there a workaround I can use to get the array's size?
std::array
. What is the error when you dogetArraySize(myX)
with the first definition? The error implies a zero length array, but your arraymyX
obviously has more than zero elements. – Bothysizeof(array) / sizeof(array[0])
as a workaround. But don't worry! Macros can help here! :D – Monodic