Note that there are some explanatory texts on larger screens.

plurals
  1. POWhat is better for the parameters of a C callback function: va_list, or ellipsis?
    primarykey
    data
    text
    <p>My library offers a callback point where my library users can register to get information. The general form of the callback is an <code>int</code> followed by various parameters whose type depend on the int value. Therefore, I defined the callback type and the function to set it as follows.</p> <pre><code>typedef void (* callback_func_t)(int type, ...); void set_callback_func(callback_func_t callback); </code></pre> <p>Within my library, I am calling this function all throughout, being the user set function, or the default one I am providing. It works.</p> <p>Now, I'd like to add a level of indirection, to be able to call several registered callbacks. The trouble is that my internal callback function that still takes ellipsis parameters, also have to call the callback function with ellipsis. As a consequence, my internal function has to interpret the <code>type</code>, unpack the parameters from the <code>va_list</code> and give them as parameters to the callbacj function.</p> <pre><code>void internal_callback(int type, ...) { va_list args; va_start(args, type); switch (type) { case 0: call_callback(type, va_arg(args, int)); break; // ... } va_end(args); } </code></pre> <p>But then, in the user's implementation of the callback, there will also be the same <code>va_list</code> usage, and interpretation of the arguments, according to the value of <code>type</code>. The solution is to pass directly the <code>va_list</code> as an argument to the callback function, making the implementation of the internal callback function obvious.</p> <pre><code>typedef void (* callback_func_t)(int type, va_list args); </code></pre> <p>My question is: is it good practice to define a callback function type that takes <code>va_list</code> as argument? I can define my callback function type as above, but what are the pros and cons compared to the definition at the top?</p>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload