传入_Generic宏的不兼容的指针类型

时间:2014-07-14 18:42:25

标签: c macros printf clang c11

以下代码生成2个警告,这些警告在问题的标题中描述。

#include <stdio.h>

static void _print_f(float *f){printf("float : %f\n", *f);}
static void _print_i(int *i)  {printf("int   : %d\n", *i);}

#define print(num) _Generic((num), \
    int*   : _print_i(num),        \
    float* : _print_f(num))


int main(void)
{
    print((&(int){10}));
    print((&(float){10.f}));

    return 0;
}

输出:

int   : 10
float : 10.000000

我知道,这个宏可以写成如下:

#define print(num) _Generic((num), \
    int*   : _print_i,             \
    float* : _print_f)(num)

并且在那种情况下,不会有任何警告,但是我的例子是我写的一个虚拟片段来证明问题。在我的真实代码库中,我选择了以前的解决方案,因为其他一些&#34;默认&#34;但需要将类型特定的参数传递给选定的函数。

所以问题是: 即使宏正常工作,输出正是我所期望的,为什么会生成警告?


标志和环境:

/* Mac OS X 10.9.4
   Apple LLVM version 5.1 (clang-503.0.40) (based on LLVM 3.4svn) */
cc -Wall -v -g -std=c11 -fmacro-backtrace-limit=0 -I/usr/local/include
   -c -o build/tmp/main.o main.c

UPDATE1:

我忘了粘贴完整的回溯!这是第一个:

main.c:39:11: warning: incompatible pointer types passing 'int *'
to parameter of type 'float *' [-Wincompatible-pointer-types]
    print((&(int){10}));
          ^~~~~~~~~~~~
main.c:31:23: note: expanded from macro 'print'
    float* : _print_f(num))
                      ^
main.c:26:29: note: passing argument to parameter 'f' here
static void _print_f(float *f){printf("float : %f\n", *f);}
                            ^

这是第二个:

main.c:40:11: warning: incompatible pointer types passing 'float *'
to parameter of type 'int *' [-Wincompatible-pointer-types]
    print((&(float){10.f}));
          ^~~~~~~~~~~~~~~~
main.c:30:23: note: expanded from macro 'print'
    int*   : _print_i(num),        \
                      ^
main.c:27:27: note: passing argument to parameter 'i' here
static void _print_i(int *i)  {printf("int   : %d\n", *i);}
                          ^

UPDATE2:

clang的开发人员修复此错误之前,这里有一个难看的解决方法来静音警告,如果assoc-list中的所有键都是类型,或者所有键都是指向类型的指针,那么它将起作用;如果类型和类型指针也在键中,则会失败:

/* HACK: re-casting pointers to mute warnings */
#define print(num) _Generic((num), \
    int*   : _print_i((int*)num),  \
    float* : _print_f((float*)num))

2 个答案:

答案 0 :(得分:12)

这不是clang中的错误,但不幸的是C11标准需要什么。 _Generic主表达式的所有分支必须是有效表达式,因此在所有情况下都有效。只评估其中一个分支的事实与此无关。

你的替代版本是C11预见的情况,因为这样:选择函数(而不是评估的调用)作为类型泛型表达式的结果,并将该函数应用于参数。

答案 1 :(得分:2)

更正:这不是(据我所知)clang中的错误,而是对_Generic应该如何表现的正确解释。仅评估泛型选择表达式中的一个通用关联,但它们都必须是有效表达式。 (_Generic不像宏一样。)

请参阅Jens Gustedt's answer


<击> 这肯定看起来像铿锵声中的一个错误。我相当确定你的代码是有效的C11。 Linux Mint上的版本3.4也是如此。

我已经整理了slightly simplified demo

#include <stdio.h>

static void print_i(int i)   { puts("int"); }
static void print_ip(int *i) { puts("int*"); }

#define print(num) _Generic((num), \
    int    : print_i(num),         \
    int*   : print_ip(num))

int main(void) {
    int i = 10;
    print(i);
    print(&i);
}

输出正确,但我收到以下警告:

c.c:12:11: warning: incompatible integer to pointer conversion passing 'int' to parameter of type 'int *'; take the address with & [-Wint-conversion]
    print(i);
          ^
          &
c.c:8:23: note: expanded from macro 'print'
    int*   : print_ip(num))
                      ^
c.c:4:27: note: passing argument to parameter 'i' here
static void print_ip(int *i) { puts("int*"); }
                          ^
c.c:13:11: warning: incompatible pointer to integer conversion passing 'int *' to parameter of type 'int'; remove & [-Wint-conversion]
    print(&i);
          ^~
c.c:7:22: note: expanded from macro 'print'
    int    : print_i(num),         \
                     ^
c.c:3:25: note: passing argument to parameter 'i' here
static void print_i(int i)   { puts("int"); }
                        ^
2 warnings generated.

<击>