|
|
|
|
|
by cyanic
2041 days ago
|
|
Named parameters using a struct. struct calculate_args {
int x, y;
enum {add=0, sub} operator;
};
int calculate_func(struct calculate_args args) {
if (args.operator == add)
return args.x + args.y;
else
return args.x - args.y;
}
#define calculate(...) calculate_func((struct calculate_args){__VA_ARGS__})
Now you can combine positional and named parameters or omit them. calculate(1, 3); // 4
calculate(8, 3, .operator=sub); // 5
calculate(.operator=sub, .y=7); // -7
Works very well in cases where there is a lot of parameters that default to 0.
Keep in mind that you still need to know how structs work and you lose compile-time error detection. |
|