Note that there are some explanatory texts on larger screens.

plurals
  1. POHow to elegantly implement a series of functions in different type versions using pure C?
    primarykey
    data
    text
    <p>I want to write several functions that are only different in the types of arguments. I know C++ has <code>template</code> to handle this problem well (not very well yet though, few compilers support <code>export</code> keyword and this keyword is queried for efficiency). For easy example, I want:</p> <pre><code>template &lt;typename T&gt; T add(T a, T b){ return a+b; } </code></pre> <p>However, in pure C (sometimes I have to choose pure C, as some platform doesn't have the C++ compiler), there have to be different function names for different versions, as</p> <pre><code>double addDouble(double a, double b){ return a+b; } int addInt(int a, int b){ return a+b; } </code></pre> <p>Hmmm, when there are two versions, it seems OK that I can do the copy-and-paste work in a source file; However, in practice there would be many lines instead of just a <code>return</code> statement in a function and would be more versions. <strong>So, my question is, how to implement a series of functions in different type versions elegantly?</strong></p> <p>So far I have tried some solutions as below, but I think they are far from good. I need your suggestions, thank you!</p> <p><strong>Solution 1:</strong> </p> <pre><code>#define mytype int mytype addInt(mytype a, mytype b){ return a+b; } #undef mytype #define mytype float mytype addFloat(mytype a, mytype b){ return a+b; } #undef mytype </code></pre> <p><strong>Shortcoming of Solution 1:</strong> duplicated contents are too many, and if I want to modify the function, I have to modify all of versions.</p> <p><strong>Solution 2:</strong></p> <p><strong>func.h</strong></p> <pre><code>#ifndef FUNC_H #define FUNC_H #define add(a, b, typename) functionAdd##typename(a,b) /* function declarations */ #define declared(typename) \ typename functionAdd##typename(typename, typename) declared(int); declared(float); #endif </code></pre> <p><strong>func.c</strong></p> <pre><code>#include "func.h" /* function code */ #define functionAdd(a, b, typename) \ typename functionAdd##typename(typename a, typename b){ \ return a+b; \ } /* function bodies (definitions) */ functionAdd(a, b, int) functionAdd(a, b, float) </code></pre> <p><strong>main.c</strong></p> <pre><code>#include &lt;stdio.h&gt; #include "func.h" int main() { int x1 = add(1, 2, int); float x2 = add(3.0, 4.0, float); printf("%d %f\n", x1, x2); return 0; } </code></pre> <p><strong>Shortcoming of Solution 2:</strong> Because the function is written in <code>define</code>, it's difficult to debug. Besides, the <code>\</code> notation is annoying. Though, it's convenient to add a new version, just inserting <code>declared(double)</code> into func.h and <code>functionAdd(a, b, double)</code> into func.c will achieve this aim.</p>
    singulars
    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.
 

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