Advertisement
Guest User

Untitled

a guest
Mar 20th, 2022
74
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 0.98 KB | None | 0 0
  1. #include <stdio.h>
  2. #include <stdlib.h>
  3.  
  4. #define template_function_name(type) \
  5.   function_##type
  6.  
  7. #define template_function_declare(type) \
  8.   type function_##type(type a, type b)  \
  9.   {                                     \
  10.     return a + b;                       \
  11.   }
  12.  
  13. #define template_function_internal(type) \
  14.     type: template_function_name(type)
  15.  
  16. template_function_declare(int);
  17. template_function_declare(float);
  18. template_function_declare(double);
  19.  
  20. #define function(a, b)                 \
  21.   _Generic(                            \
  22.     a,                                 \
  23.     template_function_internal(int),   \
  24.     template_function_internal(float), \
  25.     template_function_internal(double) \
  26.   )(a, b)
  27.  
  28. int main(void)
  29. {
  30.   printf("`int`    + `int`    = %d\n", function(10000, 10000));
  31.   printf("`float`  + `float`  = %f\n", function((float)10000.0, (float)10000.0));
  32.   printf("`double` + `double` = %f\n", function(10000.0, 10000.0));
  33.  
  34.   return EXIT_SUCCESS;
  35. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement