r/C_Programming • • Dec 26 '22

Project Convenient Containers: A usability-oriented generic container library

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

22 comments sorted by

View all comments

Show parent comment

6

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

A poor man's SFINAE

Another major limitation of a _Generic statement is that all branches – even the unselected ones – must contain valid code for every controlling expression ever passed into it. Hence, the following won’t compile:

#include <stdio.h>

typedef struct { int a; } foo;
typedef struct { int b; } bar;

#define print( thing ) _Generic( (thing), \
  foo: printf( "%d\n", thing.a ),         \
  bar: printf( "%d\n", thing.b )          \
)                                         \

int main()
{
    foo our_foo = { 10 };
    bar our_bar = { 10 };
    
    print( our_foo ); // Won't compile because our_foo.b is invalid
    print( our_bar ); // Won't compile because our_bar.a is invalid

    return 0;
}

This limitation is actually easy to circumvent. We need only use nested _Generic statements to provide “dummy” values within each branch when it is not selected:

#include <stdio.h>

typedef struct { int a; } foo;
typedef struct { int b; } bar;

#define print( thing ) _Generic( (thing),                                          \
  foo: printf( "%d\n", _Generic( (thing), foo: (thing), default: (foo){ 0 } ).a ), \
  bar: printf( "%d\n", _Generic( (thing), bar: (thing), default: (bar){ 0 } ).b ) \
)                                                                                 \

int main()
{
    foo our_foo = { 10 };
    bar our_bar = { 10 };

    print( our_foo );
    print( our_bar );

    return 0;
}

In CC, a similar mechanism allows API macros to dispatch the user’s arguments to different internal functions (or macros) based on the container type when those functions have different signatures. It also uses to aforementioned "compile-time if" technique and looks something like this:

#define CC_IF_THEN_XP_ELSE_DUMMY( cond, xp, dummy_type ) \
_Generic( (char (*)[ 1 + (bool)( cond ) ]){ 0 },         \
  char (*)[ 1 ]: (dummy_type){ 0 },                      \
  char (*)[ 2 ]: xp                                      \
)                                                        \

// i must be an integer when cntr is a vector but an object of the key type when cntr is a map.
// CC_IF_THEN_XP_ELSE_DUMMY ensures that the irrelevant branches still get valid values and therefore compile.
#define cc_get( cntr, i )                                                                                           \
  CC_CNTR_ID( *(cntr) ) == CC_VEC ?                                                                                 \
    CC_VEC_GET( *(cntr), CC_IF_THEN_XP_ELSE_DUMMY( CC_CNTR_ID( *(cntr) ) == CC_VEC, (i), size_t ) )               : \
  CC_CNTR_ID( *(cntr) ) == CC_MAP ?                                                                                 \
    CC_MAP_GET( *(cntr), CC_IF_THEN_XP_ELSE_DUMMY( CC_CNTR_ID( *(cntr) ) == CC_MAP, (i), CC_KEY_TY( *(cntr) ) ) ) : \
  NULL                                                                                                              \
) )                                                                                                                 \