By using templates:
// old function, the type is the same as Type pointer[]
// you need extra size parameter
void do_something( Type* pointer, size_t size ){ };
//this template will catch reference to array, rather then just bar pointer:
template < int size > // size must be compile time expression
inline void do_something( Type (&array) [ size ] ) // this is tricky, this & is needed
{
do_something( array, size ); // array is implicitly cast to pointer
}
//now you can use it:
Type data[10];
do_something(data);
//but when using dynamic arrays, you need to add size parameter:
int some_size = getSomeSize();
Type *pointer_to_data= new Type[some_size];
do_something(pointer_to_data,some_size);
delete[] pointer_to_data; //newer forget to delete, or use smart pointers.
You need this trick with reference to prevent array being implicit cast to pointer. Extra Template is to prevent original function being compiled many times when only the size changed.