Search code examples
cgenericsmacrosdefaultc11

C11 _Generic usage


I was trying to learn how to use the "new" C11 Generic expressions but I ran into a wall.

Consider the following code:

#include <stdlib.h>
#include <stdio.h>

#define test(X, Y, c) \
    _Generic((X), \
        double:     _Generic((Y), \
                        double * :  test_double, \
                        default: test_double \
                    ), \
        int:        _Generic((Y), \
                        int * : test_int, \
                        default: test_int \
                    ) \
    ) (X, Y, c)


int test_double(double a, double *b, int c);
int test_int(int a, int *b, int c);

int test_double(double a, double *b, int c) { return 1; }
int test_int(int a, int *b, int c) { return 2; }

int main()
{
    double *t = malloc(sizeof(double));
    int *s = malloc(sizeof(int));
    int a1 = test(3.4, t, 1);
    int i = 3;
    int a2 = test(i, s, 1);
    printf("%d\t", a1);
    printf("%d\n", a2);
    return 0;
 }

This is all perfectly working, still I don't see why those default cases in "_Generic((Y), ..." are necessary while I can omit it at the end of "_Generic((X), ..." without consequences.

In fact, if I remove those two defaults I get an error (gcc 5.4.0) saying "selector of type ‘double *’ is not compatible with any association" while macro-expanding " int a1 = test(3.4, t, 1);" and the same thing with "int *" while macro-expanding test(i, s, 1)

Is "default" really necessary or am I missing something? In the first case, why the hell should it be? If I have only test_double and test_int that can be called why should I put a default case for something that should never even compile?


Solution

  • TL;DR

    The selection happens at compile time, but this does not mean that the other (not selected) code is discarded. It still has to be valid, and this means that ...

    If default is not used and none of the type-names are compatible with the type of the controlling expression, the program will not compile.

    (Source)


    That was a surprising one:

    Without the default case for the "first Y":

    #define test(X, Y, c) \
        _Generic((X), \
            double:     _Generic((Y), \
                            double * :  test_double \
                        ), \
            int:        _Generic((Y), \
                            int * : test_int, \
                            default: test_default \
                        ) \
        ) (X, Y, c)
    

    I get that error:

    prog.c:6:30: error: '_Generic' selector of type 'int *' is not compatible with any association

    Note that it complains about an int * that is not compatible! Why? Well let's look at the reported line:

        int a2 = test(i, s, 1);
    

    X is of type int, and Y of type int *.

    Now comes the important part: Expansions happens unconditionally. So even though X is of type int, the first association for X (when it's of type double) has to be a well formed program. So with Y being an int * the following must be well formed:

    _Generic((Y), \
                 double * :  test_double \
                  ), \
    

    And since an int * is not a double *, things break here.


    I was just looking through the standard (N1570 actually) and could not find anything that actually specifies exactly this behavior, though. I guess one could report a defect in this case, the standard is too vague about this. I'm trying to do this now.