Code example:
class Foo;
typedef void (*fnptr)(Foo &foo);
fnptr gFn;
void myfoo(const Foo &foo) {}
int main() {
gFn = &myfoo;
}
The following error failed with clang:
main.cpp:9:9: error: assigning to 'fnptr' (aka 'void (*)(Foo &)') from incompatible type
'void (*)(const Foo &)': type mismatch at 1st parameter ('Foo &' vs 'const Foo &')
gFn = &myfoo;
^ ~~~~~~
1 error generated.
GCC also fails with a similar error. Passing a pointer instead of a link also
I really don't understand why this is a mistake. A function that takes const Foo & also takes Foo & as an argument, and in both cases a pointer is passed. I would like to understand why this is a mistake.
source
share