Function overloading not work with namespaces (structures) in c

I have in lib.h:

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

typedef struct LIB {
  int (*a1)(int);
  int (*a2)(int);
} lib;

int a1(int a, int b);
int a2(int a, double b);

#define a0(X, Y) _Generic((X),\
    int:   _Generic((Y),\
        int: a1,\
        double: a2\
    )\
)(X, Y)

lib* getLib();

In lib.c:

#include "lib.h"

int a1(int a, int b) {
 ...
}

int a2(int a, double b) {
 ...
}

lib* getLib() {
  lib* ret = (lib*) malloc (sizeof(lib));

  ret->a1 = a1;
  ret->a2 = a2;

  return ret;
}

In main.c:

#include "lib.h"

int main() {
  lib* l = getLib();
  int l->v = a0(1, 2.4); // error
}

Error:

error: expected identifier
note: expanded from macro 'a0'

Without define, and if a change a0 to a2 it works. How can I overload function in namespace-structure? Why I have ERROR here?



Read more here: https://stackoverflow.com/questions/66268256/function-overloading-not-work-with-namespaces-structures-in-c

Content Attribution

This content was originally published by THND at Recent Questions - Stack Overflow, and is syndicated here via their RSS feed. You can read the original post over there.

%d bloggers like this: