[Top][All Lists]

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Interest in Converting Macros to Inline Functions

From: Brent Pappas
Subject: Interest in Converting Macros to Inline Functions
Date: Mon, 14 Nov 2022 16:01:08 +0000


I noticed that Bash code sometimes uses macros where a static inline function
would appear to work equally as well.
For instance, consider the macro ALL_ELEMENT_SUB defined in array.h:

#define ALL_ELEMENT_SUB(c)      ((c) == '@' || (c) == '*')

I imagine this could be turned into a function like so:

static inline int all_element_sub(char c)       { return c == '@' || c == '*'; }

The reason why one would want to do this is because functions are often easier
to reason about than macros.
The GNU C Preprocessor manual even has a list of pitfalls one can fall into
when programming with macros.
So it may be worthwhile to turn such macros into functions to prevent
developers from accidentally falling into one of these pitfalls.

How does this idea sound?

reply via email to

[Prev in Thread] Current Thread [Next in Thread]