How (in GCC/"GNU C") do you declare a function pointer which points to an __attribute__((const))
function? The idea being that I want the compiler to avoid generating multiple calls to the function called through the function pointer when it can cache the return value from a previous call.
You can use a trailing return type in the declaration or definition of a pointer to a function. For example: auto(*fp)()->int; In this example, fp is a pointer to a function that returns int .
A pointer to a function is a pointer that points to a function. A function pointer is a pointer that either has an indeterminate value, or has a null pointer value, or points to a function. Next Page »
Function Pointer Syntax void (*foo)( int ); In this example, foo is a pointer to a function taking one argument, an integer, and that returns void. It's as if you're declaring a function called "*foo", which takes an int and returns void; now, if *foo is a function, then foo must be a pointer to a function.
Pass-by-pointer means to pass a pointer argument in the calling function to the corresponding formal parameter of the called function. The called function can modify the value of the variable to which the pointer argument points. When you use pass-by-pointer, a copy of the pointer is passed to the function.
typedef void (*t_const_function)(void) __attribute__((const));
static __attribute__((const)) void A(void) {
}
static void B(void) {
}
int main(int argc, const char* argv[]) {
t_const_function a = A;
// warning: initialization makes qualified
// function pointer from unqualified:
t_const_function b = B;
return 0;
}
Or just:
__attribute__((const)) void(*a)(void) = A;
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With