Skip to content

Compilation error when trying to cast to pointer-to-array with typeof() in C++ #152274

@Michael137

Description

@Michael137

Came up in #152113

When trying to perform following cast:

int main() {
    char *s = "abc";
    (typeof(*s)(*)[3])s;
}

Clang (and GCC FWIW), produce following error (godbolt):

main.cpp:6:18: error: expected expression
    6 |     (typeof(*s)(*)[3])s;

But this works fine in C (godbolt).

However, wrapping the typeof in another typeof works fine in both C (godbolt) and C++ (godbolt):

int main() {
    char *s = "abc";
    *(typeof(typeof(*s))(*)[3])s;
}

What's the correct behaviour here?

Metadata

Metadata

Assignees

No one assigned

    Labels

    clang:frontendLanguage frontend issues, e.g. anything involving "Sema"

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions