Skip to content

C23 constexpr + auto + type shouldn't be a valid combination #163090

@Halalaluyafail3

Description

@Halalaluyafail3

The following program demonstrates this issue:

int main(){
    constexpr auto int x={};
}

This program is invalid because:

thread_local shall not appear in the declaration specifiers of a function declaration. auto shall only
appear in the declaration specifiers of an identifier with file scope or along with other storage-class
specifiers if the type is to be inferred from an initializer.

...

If auto appears with another storage-class specifier, or if it appears in a declaration at file scope, it is
ignored for the purposes of determining a storage duration or linkage. In this case, it indicates only
that the declared type may be inferred.

Section 6.7.2 "Storage-class specifiers" Paragraphs 4 and 15 ISO/IEC 9899:2024

constexpr is a storage-class specifier, so here auto is specifying type inference (and if it wasn't, it would violate the constraint in paragraph 4). I can't find any wording to say that using a type specifier with auto for type inference is invalid, but {} surely isn't valid with type inference. Note that Clang doesn't accept the following:

auto int x={};
int main(){}

GCC also doesn't accept either program.

Metadata

Metadata

Assignees

No one assigned

    Labels

    c23clang:frontendLanguage frontend issues, e.g. anything involving "Sema"confirmedVerified by a second partygood first issuehttps://github.com/llvm/llvm-project/contribute

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions