I'm trying to develop a function with different behaviour for arrays and pointers. It could be achieved for classes by partial specialization, but it doesn't work on functions!
int i = 0;
some_func( &i ); // pointer
int arr[ 3 ] = { 0 };
some_func( arr ); // array
In second case inside of function some_func
parameter type is int*
, and there is no way to find out, that it's actually int[3]
.
On the other hand, if I use class template specialization, I'd have to specify array type explicitly:
template< typename T >
struct S
{
static void some_func( T t ) { ... }
};
template< typename T, size_t N >
struct S< T[ N ] >
{
static void some_func( T t[ N ] ) { ... }
};
// ...............
int arr[ 3 ] = { 0 };
S< int[ 3 ] >::some_func( arr ); // Works,
// but specifying type explicitly is not convenient
For now I've solved the problem with macro define (to be precise, I only need precise sizeof
for arrays).
Is there any way to solve it without turning to the dark side of macro definitions?