Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>Be aware that your generated code invokes undefined behavior. In several places, you are converting between integral types and pointer types via direct cast. This isn't legal in C. You have no guarantee that a pointer and an <code>int</code> are the same size or that you can even cast between them without altering or corrupting the value. The code may work on your particular system by coincidence, but it will break on many other systems.</p> <p>The only way that you can generically handle both pointers and integral types (as well as structures, for that matter) is to pass parameters using variable-length argument lists. That way, you can extract each argument regardless of the size of the underlying data type.</p> <p>Another option is to drop the generic <code>function</code> structure and create a customized structure for each HOF in the code. The function pointer could then list all of the necessary parameters directly instead of trying to abstract them behind a generic interface, which would eliminate the problematic casts and allow the code to work as expected regardless of the data types used.</p> <p>As far as usability goes, consider changing your interface so that the return type of the HOF is specified (or at least <em>listed</em>) on the line where it is declared. C objects always list their type in the declaration. In your example, the declaration is <code>function add1 = add(1);</code>. To find out what data type this function returns, you have to dig through the definition for the HOF. This isn't a difficult task for your sample code, but this could be non-trivial for more complex code. You may not have code for the HOF at all if it is coming from a library. Perhaps something like <code>function(int) add1 = add(1);</code> might be more explicit.</p> <p>On a side note, I recommend that you define auto-generated functions as <code>static</code> to help prevent name collisions between modules.</p>
 

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