Skip to content

Instantly share code, notes, and snippets.

@yzchen
Created January 9, 2019 19:48
Show Gist options
  • Save yzchen/090ae0d2e953f2eb54971ae46f46e542 to your computer and use it in GitHub Desktop.
Save yzchen/090ae0d2e953f2eb54971ae46f46e542 to your computer and use it in GitHub Desktop.
how to use macro to generate function names like a factory
#define CONCAT_2_EXPAND(A, B) A ## B
#define CONCAT_2(A, B) CONCAT_2_EXPAND(A, B)
#define CONCAT_3_EXPAND(A, B, C) A ## B ## C
#define CONCAT_3(A, B, C) CONCAT_3_EXPAND(A, B, C)
#define Vector_(NAME) CONCAT_3(Num, Vector_, NAME)
#define Vector CONCAT_2(Num, Vector)
#define num float
#define Num Float
struct Vector
{
num *data;
int n;
};
void Vector_(add)(Vector *C, Vector *A, Vector *B)
{
//codes
}
/*
real function after compilation :
void FloatVector_add(FloatVector *C, FloatVector *A, FloatVector *B)
{
}
*/
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment