Search code examples
ccompiler-errorsgeneric-programmingc11

Using _Generic type in C for function overloading in a nested structure


I am trying to implement a parent structure with similar functions (different parameters) and two or more child structures as members. The motto is to implement such a situation where I can call a function with the same name and depending on it's arguments(structure type), the related member function be called.

When I try to implement this as follows, the gcc compiler gives error stating that the identifier specified in _Generic is incorrect. So, what would be the correct identifier to use? How to fix this error!

P.S.: this program is actually a prototype of a larger program that I am implementing. Hence, it is intended only for clarification of my actual problem.

Thank you.

C CODE:

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

typedef struct myint{
         int mem;
}INT;

typedef struct mydouble{
      double mem;
}DOUBLE;

typedef struct head{
    INT *integer;
    DOUBLE *d_precision;
    int (*x)(INT *p);
    double (*xf)(DOUBLE *u);
}H;

int x(INT *p){
    p->mem= 2;
    return p->mem*p->mem;
}

double xf(DOUBLE *u){
    u->mem= 2.2;
    return u->mem*u->mem;
}

#define x(a) _Generic(a, struct myint*: x, DOUBLE*: xf)(a)

int main(void){
    H *ptr = (H *)malloc(sizeof(H));

    INT *i = (INT *)malloc(sizeof(INT));
    ptr->integer = i; 
    DOUBLE *f = (DOUBLE *)malloc(sizeof(INT));
    ptr->d_precision = f;

    printf("%d", (*ptr).x(ptr->d_precision));
    printf("%f", (*ptr).x(ptr->integer));

    return 0;
}

========================================================================= COMPILER OUTPUT:

root@kali:~# gcc -std=c11 -o generic3 generic3.c
generic3.c: In function ‘main’:
generic3.c:30:14: error: expected identifier before ‘_Generic’
 #define x(a) _Generic(a, struct myint*: x, DOUBLE *: xf)(a)
              ^~~~~~~~
generic3.c:40:22: note: in expansion of macro ‘x’
  printf("%d", (*ptr).x(ptr->d_precision));
                      ^
generic3.c:30:14: error: expected identifier before ‘_Generic’
 #define x(a) _Generic(a, struct myint*: x, DOUBLE *: xf)(a)
              ^~~~~~~~
generic3.c:41:22: note: in expansion of macro ‘x’
  printf("%f", (*ptr).x(ptr->integer));

Solution

  • Reading the reference for _Generic, it appears that the second parameter is a list of expressions, not arbitrary token sequences.
    In your case, _Generic is handled at the compiler level, when the preprocessed source code must already be syntactically valid, which it isn't:

    printf("%d", (*ptr)._Generic(ptr->d_precision, struct myint*: x, double: xf)(ptr->d_precision));
    //                 ^^^^^^^^^ ???