Callbacks

Joined
Feb 4, 2010
Messages
28
Reaction score
0
I do not understand why C style callbacks are not type safe. A
callback function must match the function pointer signature specified
in the callee. If the signatures are compared at compile time, how is
this not type safe? I understand that a function pointer is just an
address but if the compiler is capable of checking the use of the
callback I don't see how it could not be type safe. Please explain
and provide examples. Thanks!
 
Last edited by a moderator:
A

Alf P. Steinbach

> I do not understand why C style callbacks are not type safe. A
callback function must match the function pointer signature specified
in the callee. If the signatures are compared at compile time, how is
this not type safe? I understand that a function pointer is just an
address but if the compiler is capable of checking the use of the
callback I don't see how it could not be type safe. Please explain
and provide examples. Thanks!

Depends what you mean by "C-style callback".

If the callback is actually a C linkage function,

extern "C"
void doSomeMayhem() { std::cout << "Moo ha ha!\n"; }

then it has extern linkage so there is a slight possibility that
the definition of the function will not match the declaration.
This can occur when the function is defined in another compilation
unit. It can happen because with C linkage function as above you get
less linkage level name ornamentation, so called name mangling, hence
less information about args etc., and almost anything will match.

A simple solution is to not use actual C functions but C++ functions,
or if you (must) use C functions, not ones defined in other compilation
units.
 
Last edited by a moderator:
S

Siemel Naran

> I do not understand why C style callbacks are not type safe. A
callback function must match the function pointer signature specified
in the callee. If the signatures are compared at compile time, how is
this not type safe? I understand that a function pointer is just an
address but if the compiler is capable of checking the use of the
callback I don't see how it could not be type safe. Please explain
and provide examples. Thanks!

Callbacks are inherently safe, and I use them in C++ too to avoid code
bloat.

But sometimes, because C does not have templates, we may use void* to
represent any types. An example is std::qsort.

void qsort(void *base, size_t nelem, size_t width, int (*fcmp)(const void *,
const void *));
 
Last edited by a moderator:
J

John Harrison

>I do not understand why C style callbacks are not type safe. A
callback function must match the function pointer signature specified
in the callee. If the signatures are compared at compile time, how is
this not type safe? I understand that a function pointer is just an
address but if the compiler is capable of checking the use of the
callback I don't see how it could not be type safe. Please explain
and provide examples. Thanks!

A callback is type safe for the reasons you mention. Probably what you are
thinking of is that C often forces you to use them in a type unsafe manner.
E.g. the standard C function qsort

void qsort(void*, size_t, size_t, int (*comp)(const void*, const void*));

Because qsort sorts arrays of any POD type, the callback function cannot
specify what the types of its arguments are. Hence this usage is not type
safe.

Of course C++ offers alternatives that are type safe.

john
 
Last edited by a moderator:

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,995
Messages
2,570,230
Members
46,819
Latest member
masterdaster

Latest Threads

Top