Answer the question
In order to leave comments, you need to log in
Why does a warning appear in _Generic on cast when overloading a function through a macro?
There is a code in which a function is overloaded through a macro.
The compiler issues a warning on cast variables, but the compiled program works correctly.
Suppressing warnings is not an option.
I would like to know the reason for the warnings, is this the standard behavior or the imperfection of the preprocessor?
Minimum code for testing and experimentation.
#include <inttypes.h>
#include <stdint.h>
#include <stdio.h>
#define FNK(...) \
FFF_x(, \
##__VA_ARGS__, \
FFF_B(__VA_ARGS__), \
FFF_A(__VA_ARGS__), \
FFF_0(__VA_ARGS__))
#define FFF_x(x, A, B, FFF, ...)FFF
#define FFF_0() fnk(0, 0, NULL)
#define FFF_A(A) fnk(A, 0, NULL)
#define FFF_B(A, BC) \
_Generic((A), \
int : _Generic((BC), \
int : fnk(A, BC, NULL), \
char * : fnk(A, 0, BC)))
void fnk(uintmax_t A, uintmax_t B, char* C){
printf("A : %"PRIXMAX"\n", A);
printf("B : %"PRIXMAX"\n", B);
printf("C : \"%s\"\n", C);
}
void fnk_test(void){
printf("\n0)---------\n");
FNK();
printf("\n1)---------\n");
FNK(1);
printf("\n2.1)---------\n");
FNK(1, 2);
printf("\n2.2)---------\n");
FNK(1, "2");
}
int main() {
fnk_test();
return 0;
}
0)---------
A : 0
B : 0
C : "(null)"
1)---------
A : 1
B : 0
C : "(null)"
2.1)---------
A : 1
B : 2
C : "(null)"
2.2)---------
A : 1
B : 0
C : "2"
Answer the question
In order to leave comments, you need to log in
I would like to know the reason for the warnings, is this the standard behavior or the imperfection of the preprocessor?
_Generic((1), int : _Generic((2), int : fnk(1, 2,
# 37 "generic.c" 3 4
((void *)0)
# 37 "generic.c"
), char * : fnk(1, 0, 2)));
printf("\n2.2)---------\n");
_Generic((1), int : _Generic(("2"), int : fnk(1, "2",
# 40 "generic.c" 3 4
((void *)0)
# 40 "generic.c"
), char * : fnk(1, 0, "2")));
void fnk_int(uintmax_t A, uintmax_t B){
...
}
void fnk_pchar(uintmax_t A, char *B){
...
}
#define FFF_B(A, BC) \
_Generic((A), \
int : _Generic((BC), \
int : fnk_int, \
char * : fnk_pchar)(A, BC))
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question