Skip to content

C23 Clang rejects valid code involving compound literal and typeof #143613

@dcawley15

Description

@dcawley15

I was attempting to create a generic bit cast macro using the new C23 typeof feature. Clang appears to reject the following code with "error: initializer element is not a compile-time constant" even though I believe this code is valid in C23. Both GCC and MSVC (with /std:clatest) accept this code.

#include <stdint.h>

#define bitcast(type, value) \
    (((union{ typeof(value) src; type dst; }){ (value) }).dst)

int main(void)
{
    int64_t foo = 4;
    double bar = bitcast(double, foo); // ok
    foo = bitcast(int64_t, bar); // ok
    foo = bitcast(int64_t, bitcast(double, foo)); // error
    return 0;
}

Compiler Flags: -std=c23 -pedantic

https://godbolt.org/z/zsfErTo1e

Metadata

Metadata

Assignees

No one assigned

    Labels

    c23clang:frontendLanguage frontend issues, e.g. anything involving "Sema"duplicateResolved as duplicate

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions