I have this overloaded function in C++:
template<std::size_t N>
void func(const int(&)[N]) {
std::cout << "1";
}
void func(const int*) {
std::cout << "2";
}
If I call func with a pointer, the second overload is called. This is expected.
const int* p{};
func(p);
// prints 2 - OK
If I call func with an int array, the first overload is called. This is also expected.
int ints[]{1, 2, 3, 4, 5};
func(ints);
// prints 1 - OK
However, if I have call func with an array of const ints, I expect the first overload to be called, but the second is called instead.
const int ints[]{1, 2, 3, 4, 5};
func(ints);
// prints 2 - NOT OK, 1 is expected
My question is why and how could I write the function func to have the first overload called for const arrays as well? I need the array size in a template parameter, so anything like strlen is not a possible solution for me.
(It behaves the same way for other types too, like std::string.)