r/C_Programming • • Dec 26 '22

Project Convenient Containers: A usability-oriented generic container library

https://github.com/JacksonAllan/CC
16 Upvotes

22 comments sorted by

View all comments

Show parent comment

7

u/jacksaccountonreddit Dec 28 '22 edited Dec 30 '22

Compile-time if

_Generic can be abused to create compile-time conditional expressions whose type depends on the condition:

#define COMPTIME_IF( cond, on_true, on_false )   \
_Generic( (char (*)[ 1 + (bool)( cond ) ]){ 0 }, \
  char (*)[ 1 ]: on_false,                       \
  char (*)[ 2 ]: on_true                         \
)                                                \

CC uses this mechanism in various places, e.g. when an API macro must return either a bool or a pointer to an element depending on the container type.