I'm not asking if it's possible to #define
a macro inside a function. I understand that it is possible and that macros are preprocessing mechanisms that are almost copy-paste (to an extent). What I'm asking is are there broad reasons to avoid making a macro for use in a single function or is it simply up to the codebase maintainers as to what they decide is good style?
Here is a contrived example similar to my usecase:
int main() {
/* ... */
#define POS(x, y) grid_array[width*(y) + (x)]
/* Use POS(x, y) in this function */
#undef POS
/* ... */
}
Another way to put the question is: Would other C developers nod their head in understanding or shake their head in distain?
Edit:
This is not a duplicate of the question "Macro vs function." I understand (some of) the differences between the two, e.g. MIN(a, b) (a)>(b)?(b):(a)
evaluates a
and b
twice. I'm asking if it is good practice to use a macro for a single function.
The answer (and comments) pointed out that my simple example doesn't merit using a macro. Though my actual use case is not that simple, I have to agree. The "saved typing" doesn't merit convoluted code.
Here's my actual use case if you were curious:
/* array represents a graph with max degree 2. These operations are domain specific. */
#define CONNECT(a, b, i) {array[8*(a) + i] = b; array[8*(b) + (i+4)%8] = a;}