Consider the following code
#include <iostream>
#include <cstring>
template <int size>
void func(const char (&arr)[size])
{
//Prefer this function for const string literals.
std::cout << "Array of " << size - 1 << " bytes" << std::endl;
}
void func(const char *arr)
{
//Use this for all other strings that are not literals
std::cout << "String of " << strlen(arr) << " bytes" <<std::endl;
}
int main()
{
func("Hello!");
return 0;
}
How do I ensure that the template function is called in the example instead of the overload?
What I would like it that the templated function is called on string literals while the other is available for all other cases. With the templated function, i can avoid calling the strlen(). In my application both functions finally create a string_view object.
I would like to know how an overload is preferred.