3 ms·
The main issue comes from code like this (https://godbolt.org/z/GY5baP18q https://godbolt.org/z/GY5baP18q): #include <stdio.h> typedef int int_func(int
by LegionMammal978 5y ago
The main issue comes from code like this (https://godbolt.org/z/GY5baP18q https://godbolt.org/z/GY5baP18q):
#include <stdio.h>
typedef int int_func(int x);
void print_result(int_func *func, int x) { printf("%d\n", func(x)); }
int f(int x) { return x; }
_Alias f_alias = f;
static int_func * const f_ptr = f;
void doesnt_work(int x) {
print_result(&f, x);
print_result(&f_alias, x);
//print_result(&f_ptr, x);
//error: incompatible pointer types
}
Since &f is of type int_func *, an alias to f must behave such that taking its address still results in an int_func *. Otherwise, existing code is broken, as shown above, since f cannot be replaced with f_ptr in the expression.
This is what _Alias is meant to solve: it's perfectly transparent, in that the end-user has no way to distinguish whether the name comes from the original declaration or an _Alias. This allows libraries to modify the external name of a function, without breaking code that depends on the syntactic name of the function.
- mananaysiempre 5y agoNot saying it’s pretty or perfect, but you can make it work (https://godbolt.org/z/asW3zvxa9 https://godbolt.org/z/asW3zvxa9): #define f_deref_ptr (*f_ptr) /* ... */ print_result(&f_deref_ptr, x); Function types (as opposed to function pointer types), along with array types, are kind of shy in C, but it is possible to get hold of and use them.