Would that function technically be capable of acting on arbitrary triples (a, b, g)
of pointers of the form type_a *a
, type_b *b
, rtype (\*g)(type_a *, type_b *)
Yes.
without introducing errors?
Oh. Well...that all comes down to the implementation of whatever g
is pointing to. Unfortunately, as soon as you use void *
for anything, you've thrown any concept of type safety out the window and into oncoming traffic.
This is pretty much how the qsort
standard library function works - you pass it a pointer to your array (as a void *
), the number of elements, the size of each element, and a pointer to a function that compares two elements:
void qsort(void *base, size_t nmemb, size_t size, int (*compar)(const void *, const void *));
If you want to sort an array of int
in ascending order, you write a comparison function like so:
int compare_int( const void *l, const void *r )
{
const int *a = l;
const int *b = r;
if ( *a < *b )
return -1;
else if ( *a > *b )
return 1;
return 0;
}
int main( void )
{
int arr[100];
...
qsort( arr, 100, sizeof arr[0], compare_int );
}
We can do the same thing for an array of double
:
int compare_dbl( const void *l, const void *r )
{
const double *a = l;
const double *b = r;
if ( *a < *b )
return -1;
else if ( *a > *b )
return 1;
return 0;
}
int main( void )
{
double arr[100];
...
qsort( arr, 100, sizeof arr[0], compare_dbl );
}
The compare_int
and compare_dbl
functions are responsible for converting the arguments to the appropriate type for comparison. But here's the problem - there's nothing stopping you from doing this:
int arr[100];
...
qsort( arr, 100, sizeof arr[0], compare_dbl );
There's no kind of compile-time or run-time check to make sure you've passed the right comparison function for the type of array you're trying to sort. Since both qsort
and your comparison functions use void *
for everything, there's no way for the compiler to know you're passing the wrong comparison function for the type of the array that you're using.
Yes, you can act on combinations of arbitrary types. No, you can't guarantee that doing so will not introduce any errors.