Reputation: 753
I have some code dispatching a function based on the type of its argument, using the _Generic. I do not understand the warning gcc generates.
Compile with gcc main.c
#include <stdio.h>
#define FOOBAR(x) _Generic((x), \
int *: foo(x), \
long *: bar(x) \
)
void foo(int* x) {
printf("foo\n");
}
void bar(long* y) {
printf("bar\n");
}
int main() {
int a = 1111;
long b = 2222;
FOOBAR(&a);
FOOBAR(&b);
}
Now, this code does compile and _Generic works as expected, i.e. "foo" appears then "bar". However, the compiler (gcc and clang) generate a weird warning that looks like its matching the argument against _Generic to the wrong line:
main.c: In function ‘main’:
main.c:20:12: warning: passing argument 1 of ‘bar’ from incompatible pointer type [-Wincompatible-pointer-types]
20 | FOOBAR(&a);
| ^~
| |
| int *
main.c:5:15: note: in definition of macro ‘FOOBAR’
5 | long *: bar(x) \
| ^
main.c:12:16: note: expected ‘long int *’ but argument is of type ‘int *’
12 | void bar(long* y) {
| ~~~~~~^
main.c:21:12: warning: passing argument 1 of ‘foo’ from incompatible pointer type [-Wincompatible-pointer-types]
21 | FOOBAR(&b);
| ^~
| |
| long int *
main.c:4:14: note: in definition of macro ‘FOOBAR’
4 | int *: foo(x), \
| ^
main.c:8:15: note: expected ‘int *’ but argument is of type ‘long int *’
8 | void foo(int* x) {
| ~~~~~^
Two warnings are generated, one for each FOOBAR. It appears to be passing &a
which is int * to bar which takes a long *, and vice versa for &b
.
(Comment out the one of the FOOBARs to see just one incompatible pointer error.)
Why is gcc warning me that _Generic is dispatching its arg to the wrong function?
I know this isn't normally how people use _Generic, i.e. the argument list would usually be outside the _Generic(). But I have some use cases for dispatching to functions that take different number of arguments.
Upvotes: 3
Views: 581
Reputation: 754030
Note that the example in the standard §6.5.1.1 Generic selection is:
5 EXAMPLE The cbrt type-generic macro could be implemented as follows:
#define cbrt(X) _Generic((X), \ long double: cbrtl, \ default: cbrt, \ float: cbrtf \ )(X)
Note where the parentheses for the function invocation are — outside the _Generic(…)
part of the generic selection.
Adapting that to your code:
#include <stdio.h>
#define FOOBAR(x) _Generic((x), \
int *: foo, \
long *: bar \
)(x)
static void foo(int *x)
{
printf("foo (%d)\n", *x);
}
static void bar(long *y)
{
printf("bar (%ld)\n", *y);
}
int main(void)
{
int a = 1111;
long b = 2222;
FOOBAR(&a);
FOOBAR(&b);
return 0;
}
This compiles cleanly with GCC 10.2.0 set fussy (source file gs31.c
):
$ gcc -O3 -g -std=c11 -Wall -Wextra -Werror -Wmissing-prototypes -Wstrict-prototypes -fno-common -c gs31.c
$
The code changes outside the FOOBAR
macro avoid compilation warnings requested by my standard compiling options.
The output from the C pre-processor for your code is:
int main() {
int a = 1111;
long b = 2222;
_Generic((&a), int *: foo(&a), long *: bar(&a) );
_Generic((&b), int *: foo(&b), long *: bar(&b) );
}
compared with:
int main(void)
{
int a = 1111;
long b = 2222;
_Generic((&a), int *: foo, long *: bar )(&a);
_Generic((&b), int *: foo, long *: bar )(&b);
}
The difference is that your code calls foo()
with a long *
(aka &b
) and bar()
with an int *
(aka &a
), and this is what (correctly) triggers the warnings.
Upvotes: 7