I was wondering if const qualifying function pointers makes any difference, since the only meaning i could think of is auto const-qualifying its parameters, which is of course not the case.
I created a little example file (test.c):
typedef void* vop(void*);
vop fn;
const vop cfn;
int main(void){
vop *p_fn = fn;
const vop *cp_fn = fn; // <- gives compiler warning
vop *p_cfn = cfn;
const vop *cp_cfn = cfn;
}
and ran
gcc -Wall -Wno-unused-variable -c test.c
which yields the following warning:
warning: initialization makes '__attribute__((const))' qualified function pointer from unqualified [-Wdiscarded-qualifiers]
So it is "ok" to assign a "pointer to const vop" to a variable of type "pointer to vop" which, if it was not a function pointer would yield something like:
warning: initialization discards 'const' qualifier from pointer target type [-Wdiscarded-qualifiers]
But now it warns for the opposite case. So the question arises: What is the difference between const qualified function pointers and those that are not const qualified?
Note: The cppreference has the following paragraph:
If a function type is declared with the const type qualifier (through the use of typedef), the behavior is undefined.
Is the warning i saw a result of that "undefined behaviour" or does this paragraph not apply in this case (and if not, in what case can it be applied)?