In the following implementation of static_strlen, why are the & and parentheses around str necessary
Posted
by Ben
on Stack Overflow
See other posts from Stack Overflow
or by Ben
Published on 2010-04-01T21:12:09Z
Indexed on
2010/04/01
21:23 UTC
Read the original article
Hit count: 286
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;
}
© Stack Overflow or respective owner