c - _Generic 发出奇怪的 gcc 警告

标签 c macros

我有一些代码使用 _Generic 根据参数的类型分派(dispatch)函数。我不明白 gcc 生成的警告。

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);
}

现在,这段代码可以编译并且 _Generic 按预期工作,即“foo”出现然后“bar”。然而,编译器(gcc 和 clang)生成了一个奇怪的警告,看起来它匹配反对 _Generic 的参数到错误的行:

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) {
      |          ~~~~~^

生成两个警告,每个 FOOBAR 一个。它似乎将 &a 是 int * 传递给需要 long * 的 bar,反之亦然 &b。 (注释掉其中一个 FOOBAR,只看到一个不兼容的指针错误。)

为什么 gcc 警告我 _Generic 正在将其 arg 分配给错误的函数?


我知道这通常不是人们使用 _Generic 的方式,即参数列表通常在 _Generic()之外。但是我有一些用例可以分派(dispatch)到采用不同数量参数的函数。

最佳答案

请注意标准中的示例 §6.5.1.1 Generic selection是:

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)

请注意函数调用的括号所在的位置 — 在通用选择的 _Generic(...) 部分之外。

根据您的代码进行调整:

#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;
}

使用 GCC 10.2.0 编译干净(源文件 gs31.c):

$ gcc -O3 -g -std=c11 -Wall -Wextra -Werror -Wmissing-prototypes -Wstrict-prototypes -fno-common -c gs31.c
$

FOOBAR 宏之外的代码更改避免了我的标准编译选项请求的编译警告。

C 预处理器为您的代码输出的是:

int main() {
    int a = 1111;
    long b = 2222;

    _Generic((&a), int *: foo(&a), long *: bar(&a) );
    _Generic((&b), int *: foo(&b), long *: bar(&b) );
}

相比于:

int main(void)
{
    int a = 1111;
    long b = 2222;

    _Generic((&a), int *: foo, long *: bar )(&a);
    _Generic((&b), int *: foo, long *: bar )(&b);
}

区别在于您的代码调用 foo() 时使用的是 long *(又名 &b)和 bar() 带有 int *(又名 &a),这就是(正确)触发警告的原因。

关于c - _Generic 发出奇怪的 gcc 警告,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/68309201/

相关文章:

c - 函数返回类型语法

c - 可变函数问题

gcc - 如何使用 gcc 在命令行取消定义定义

macros - 为什么这个 lisp 递归宏不起作用?

c++ - 了解用于在 OpenGL 3.3+ 核心中使用三角形绘制四边形的代码

c - 输入缓冲区获取输入,C 编程

c - 指针和cstring初学者的烦恼

c - 编译时设置共享库前缀

c++ - C++ 中优雅的函数定义

c 预处理器宏在扩展后连接参数