Skip to content

Instantly share code, notes, and snippets.

@alex-robert-fr
Created April 13, 2023 12:31
Show Gist options
  • Save alex-robert-fr/cb5e35db4876c1e28a0d19800fe9d66f to your computer and use it in GitHub Desktop.
Save alex-robert-fr/cb5e35db4876c1e28a0d19800fe9d66f to your computer and use it in GitHub Desktop.
Template in C
#include<stdio.h>
#define TLIST(T,L) \
struct _List##L { \
struct _List##L * _next; \
struct _List##L * _prev; \
T _data; \
} L = {&(L), &(L)}
#define TOTO(T, L) T L
#define SUM_TEMPLATE(T) T sum_##T(T a, T b) { return ( a + b ); }
SUM_TEMPLATE(int)
SUM_TEMPLATE(float)
#define GET_TYPE_STR(T) _Generic( (T), int: "int", float: "float", default: "")
#define SUM(a, b) sum_##GET_TYPE_STR(a)(a,b)
int main(void)
{
printf("%d\n", sum_int(2, 2));
printf("%f\n", sum_float(2.4, 2.4));
printf("%d\n", SUM(2, 2));
return (0);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment