I have read the following links how to calculate size of an array through template function:
- How does this “size of array” template function work? [duplicate]
- Can someone explain this template code that gives me the size of an array? [duplicate]
Magic arguments in function templates
But this trick doesn't work in case of zero array. Why is the following code not correct? For instance the online compiler ideone prints the following error message:
error: no matching function for call to 'size_of_array(int [0])'
std::size_t num = size_of_array(arr);
#include <cstddef>
template <typename T, std::size_t N>
inline std::size_t size_of_array( T (&)[N] ) {
return N;
}
int main()
{
int arr[0]={};
std::size_t num = size_of_array(arr);
}