I am trying a function's return value to be to a reference to an array whose size is variable (i.e. templated). The code I have is:
const char *strarr[] = { "one", "two", "three" };
template <std::size_t T>
const char* (&GetArr())[T]
{
return strarr;
}
The reason I want to do it this way: it would be convenient in my case to then use in a a loop such that:
for (const auto& s : GetArr()) { std::cout << s << std::endl; }
but this wouldn't compile. the only way to use it is to specify the size in GetArr(); but I was wondering why it can't deduce the size by itself, as it would have if I passed a reference to this array as a parameter to a templated function, as in:
template <std::size_t T>
void PrintArray(const char* (&arr)[T])
{
for (const auto& s : arr)
{
std::cout << s << std::endl;
}
}
which does work.