Incompatible pointer types passing in _Generic macro

前端 未结 2 953
无人及你
无人及你 2020-11-28 14:38

The following code generates 2 warnings which are described in the question\'s title.

#include 

static void _print_f(float *f){printf(\"float         


        
2条回答
  •  爱一瞬间的悲伤
    2020-11-28 14:57

    CORRECTION : This is not (as far as I can tell) a bug in clang, but a correct interpretation of how _Generic is supposed to behave. Only one of the generic associations in a generic-selection expression is evaluated, but all of them must be valid expressions. (_Generic does not act like a macro.)

    See Jens Gustedt's answer.


    This definitely looks like a bug in clang. I'm reasonably sure your code is valid C11. I see the same thing with version 3.4 on Linux Mint.

    I've put together a slightly simplified demo :

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

    The output is correct, but I get the following warnings:

    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.
    

提交回复
热议问题