_Generic(3)

NAME
_Generic - type-generic selection

SYNOPSIS
_Generic(expression, type1: e1, ... /*, default: e */);

DESCRIPTION
_Generic() evaluates the path of code under the type selector that is compatible with the type of the controlling expression, or default: if no type is compatible.

expression is not evaluated.

This is especially useful for writing type-generic macros, that will behave differently depending on the type of the argument.

STANDARDS
C11 and later.

EXAMPLES
The following code demonstrates how to write a macro similar to C++'s static_cast(), which will allow casting safely between a limited set of types. It is useful for example when calling system calls or library functions that use compatible structures, like for example bind(2) with sockaddr(3type).

        /* This code is in the public domain. */
        #include <netinet/in.h>
        #include <sys/socket.h>
        #include <sys/un.h>
        #define sockaddr_cast(t, p)                            \
            _Generic(&*(p),                                    \
            struct sockaddr *:                                 \
                _Generic((typeof_unqual(t)) NULL,              \
                struct sockaddr_in *:             (t) (p),     \
                struct sockaddr_in6 *:            (t) (p),     \
                struct sockaddr_un *:             (t) (p),     \
                default:                              (p)),    \
            struct sockaddr **:                                \
                _Generic((typeof_unqual(t)) NULL,              \
                struct sockaddr_in **:            (t) (p),     \
                struct sockaddr_in6 **:           (t) (p),     \
                struct sockaddr_un **:            (t) (p),     \
                default:                              (p)),    \
            const struct sockaddr *:                           \
                _Generic((t) NULL,                             \
                const struct sockaddr_in *:       (t) (p),     \
                const struct sockaddr_in6 *:      (t) (p),     \
                const struct sockaddr_un *:       (t) (p),     \
                default:                              (p)),    \
                                                               \
            struct sockaddr_in *:                              \
                _Generic((typeof_unqual(t)) NULL,              \
                struct sockaddr *:                (t) (p),     \
                default:                              (p)),    \
            struct sockaddr_in **:                             \
                _Generic((typeof_unqual(t)) NULL,              \
                struct sockaddr **:               (t) (p),     \
                default:                              (p)),    \
            const struct sockaddr_in *:                        \
                _Generic((t) NULL,                             \
                const struct sockaddr *:          (t) (p),     \
                default:                              (p)),    \
                                                               \
            struct sockaddr_in6 *:                             \
                _Generic((typeof_unqual(t)) NULL,              \
                struct sockaddr *:                (t) (p),     \
                default:                              (p)),    \
            struct sockaddr_in6 **:                            \
                _Generic((typeof_unqual(t)) NULL,              \
                struct sockaddr **:               (t) (p),     \
                default:                              (p)),    \
            const struct sockaddr_in6 *:                       \
                _Generic((t) NULL,                             \
                const struct sockaddr *:          (t) (p),     \
                default:                              (p)),    \
                                                               \
            struct sockaddr_un *:                              \
                _Generic((typeof_unqual(t)) NULL,              \
                struct sockaddr *:                (t) (p),     \
                default:                              (p)),    \
            struct sockaddr_un **:                             \
                _Generic((typeof_unqual(t)) NULL,              \
                struct sockaddr **:               (t) (p),     \
                default:                              (p)),    \
            const struct sockaddr_un *:                        \
                _Generic((t) NULL,                             \
                const struct sockaddr *:          (t) (p),     \
                default:                              (p)),    \
                                                               \
            default:                                           \
                (p)                                            \
            )
        socklen_t           slen;
        struct sockaddr_un  sun;
        slen = sizeof(ss);
        getsockname(sfd, sockaddr_cast(struct sockaddr *, &sun), &slen);

The following program demonstrates how to write a replacement for the standard imaxabs(3) function, which being a function can't really provide what it promises: seamlessly upgrading to the widest available type.

        #include <stdint.h>
        #include <stdio.h>
        #include <stdlib.h>
        #define my_imaxabs  _Generic(INTMAX_C(0),  \
            long:           labs,                  \
            long long:      llabs                  \
         /* long long long: lllabs */              \
        )
        int
        main(void)
        {
            off_t  a;
            a = -42;
            printf("imaxabs(%jd) == %jd\n", (intmax_t) a, my_imaxabs(a));
            printf("&imaxabs == %p\n", &my_imaxabs);
            printf("&labs    == %p\n", &labs);
            printf("&llabs   == %p\n", &llabs);
            exit(EXIT_SUCCESS);
        }