In the following implementation of static_strlen, why are the & and parentheses around str necessary
- by Ben
If I change the type to const char str[Len], I get the following error:
error: no matching function for call to ‘static_strlen(const char [5])’
Am I correct that static_strlen expects an array of const char references? My understanding is that arrays are passed as pointers anyway, so what need is there for the elements to be references? Or is that interpretation completely off-the-mark?
#include <iostream>
template <size_t Len>
size_t
static_strlen(const char (&str)[Len])
{
return Len - 1;
}
int main() {
std::cout << static_strlen("oyez") << std::endl;
return 0;
}