genericsdynamic-arraysc11

Why is This _Generic Statement Giving an "Expression expected" Error?


I am writing an implementation of a dynamic array in C11. The keyword _Generic comes along with the C11 standard and is supported in my version of GCC (14.1.1) as far as I know. I have worked with it before, and it has never given me any trouble before. However, now, no matter what I input into it, it seems to give me the error Expected expression.

I have a feeling it's a stupid mistake I have no right making after all my time working with C, but I swear I've checked everything, and nothing fixes it.

typedef enum
{
    unsigned8,
    unsigned16,
    unsigned32,
    signed8,
    signed16,
    signed32,
    string,
    unknown
} dynamic_array_type_t;

#define TYPE_TO_DARRAY_TYPE(type) \
    _Generic((type),              \
        uint8_t: unsigned8,       \
        uint16_t: unsigned16,     \
        uint32_t: unsigned32,     \
        int8_t: signed8,          \
        int16_t: signed16,        \
        int32_t: signed32,        \
        char*: string,            \
        default: unknown)

Where have I messed up? Is this just a problem with the way I've set up my compiler/DE? For context, this is one of the ways this functionality is implemented:

#define CreateDynamicArray(type, size)                                    \
    InternalCreateDynamicArray(TYPE_TO_DARRAY_TYPE(type), size)

dynamic_array_t*
InternalCreateDynamicArray(dynamic_array_type_t array_type, uint32_t size);

Solution

  • Turns out that I am...not the sharpest tool in the shed. _Generic takes in a specific instance of a type, not the type itself.

    TYPE_TO_DARRAY_TYPE(char*) // Failure: "expected expression"
    TYPE_TO_DARRAY_TYPE("e")   // Success.