1

When trying to implement a _Generic call that has both type bool and a struct, the struct is somehow caught in the bool case and an error is received.

Error(s):
source_file.c:27:14: error: used type 'struct sockaddr' where arithmetic or pointer type is required
printf("%s", log_param_encode(tempS));
             ^                ~~~~~
source_file.c:6:54: note: expanded from macro 'log_param_encode'
                                            bool: (x ? "true" : "false"), \

if i change the bool case to include only a string such as "true", it works fine and i receive the "sockaddr" in the printf output.

the code:

#include <stdio.h>

#include <stdbool.h>
#include <sys/socket.h>

#define log_param_encode(x) _Generic((x), \
                                  bool: (x ? "true" : "false"), \
                                  struct sockaddr: "sockaddr", \
                                  default: x)

int main(void)
{
    struct sockaddr tempS;
    printf("%s", log_param_encode(tempS));
    return 0;
}

How is the _Generic expansion happening? why is it trying to use X in a case it isn't for?

Itay Bianco
  • 697
  • 6
  • 16
  • I think this might be a compiler bug... From section 6.5.1.1 of the C11 draft: *None of the expressions from any other generic association of the generic selection is evaluated.* but it appears to be at least type-checking them. Clang 6 does the same. – Shawn Jan 27 '19 at 14:01
  • 1
    @Shawn - Not evaluated is not the same has not having to be correct in terms of semantics and constraints. Those have to be valid expressions. – StoryTeller - Unslander Monica Jan 27 '19 at 14:03

0 Answers0