Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>I think this may be closer to what you want, but I am unsure. My understanding is the idea is to allow c functions with arbitrary numbers of arguments, that are type checked and can be eliminated at compile time.</p> <p>Let me warn you about the use of underscores in identifiers, by quoting the standard. You may run into a reserved identifier. However, the likelihood of this is unknown to me.</p> <p>ISO/IEC 9899:1999 (E) 7.1.3</p> <blockquote> <p>— All identifiers that begin with an underscore and either an uppercase letter or another underscore are always reserved for any use.</p> </blockquote> <p>This solution requires GCC. The version of GCC must also support weak symbols. The idea is to allow the compiler to look for the right function definition using weak symbols. Additionally the contents of the function are simplified by using the knowledge that the compiler should prune dead branches, ie:</p> <pre><code>if (0) { ... } </code></pre> <p>at compile time without further analysis (GCC 4.x certainly does this). By defining the non-existent optional parameters as c pre-processor (cpp) symbolsyou can avoid having cpp conditionals in the function body (if so desired). See how opt1 and opt2 are defined for f_opt0 below.</p> <pre><code>#include &lt;assert.h&gt; #include &lt;stdio.h&gt; extern void f_opt0(int a, int b) __attribute__((weak)); extern void f_opt1(int a, int b, int opt1) __attribute__((weak)); extern void f_opt2(int a, int b, int opt1, int opt2) __attribute__((weak)); #ifdef OPT0 void f_opt0(int a, int b) { #define opt1 0 #define opt2 0 #endif #ifdef OPT1 void f_opt1(int a, int b, int opt1) { #define opt2 0 #endif #ifdef OPT2 void f_opt2(int a, int b, int opt1, int opt2) { #endif if (opt1) printf("opt1=%d\n", opt1); if (opt2) printf("opt2=%d\n", opt2); printf("a+b=%d\n", a+b); #undef opt1 #undef opt2 } #define f(a, b, o1, o2) \ if (f_opt2) f_opt2(a, b, o1, o2); \ else if (f_opt1) f_opt1(a, b, o1); \ else if (f_opt0) f_opt0(a, b); \ else { assert(0 &amp;&amp; "no f() defined!"); } int main(void) { f(1, 2, 1, 1); return 0; } </code></pre> <p>My testing was very limited, and I do not advocate this as good design in C. It seems prone to problems and is troublesome to comprehend. However, I hope it addresses your goals.</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