Best practice for "is value in list" macro in C without relying on sentinel values?
I'm looking for a clean and robust way to implement a membership check for a list of integers in C. I currently use a variadic macro that creates a temporary array on the stack and uses a sentinel value to mark the end of the list: static inline bool int__in(int val, const int *arr) { while (*arr != INT_MIN) { if (val == *arr++) return true; } return false; } #define int_in(val, ...) int__in((val), (const int []) { __VA_ARGS__, INT_MIN }) Using INT_MIN as a sentinel is obviously problematic because INT_MIN can be a valid data point in many applications. However, since C lacks reflection, there
0 upvotes
View original discussion ↗
Comments
Sign in or sign up to comment and upvote.
No comments yet.