Motivation:
Almost for fun, I am trying to write a function overload that can tell apart whether the argument is a fixed-size array or a pointer.>
This seems to work for me
#include
template
std::enable_if_t::value>
foo(T) { std::cout << "pointer\n"; }
template
void foo(T(&)[sz]) { std::cout << "array\n"; }
int main()
{
char const* c;
foo(c);
foo("hello");
}
Bonus std::experimental::type_traits
:
using std::experimental::is_pointer_v;
std::enable_if_t>
Your comment made me try something even simpler
template void foo(T) { std::cout << "pointer\n"; }
template void foo(T(&)[sz]) { std::cout << "array\n"; }
Of course the problem here is that foo
is now callable for any type, depends on how lax you want your parameter checking to be.
One other way is to (ab)use rvalue references
void foo(char const*&) { std::cout << "pointer\n"; }
void foo(char const*&&) { std::cout << "array\n"; }
Obviously it's not foolproof.