Skip to content

[clang] Constexpr function of a function parameter cannot be used in a constant expression #118063

@StefanPaulet

Description

@StefanPaulet

The following code compiles on gcc, but fails on msvc and clang:

template <unsigned int N>
struct array {
    constexpr auto size() const -> unsigned int {
        return N;
    }
};

constexpr auto foo(array<5> const& arr) {
    return array<arr.size()>{}.size();
}

constexpr auto bar(array<5> arr) {
    return array<arr.size()>{}.size();
}

int main()
{
    array<5> arr {};
    static_assert(arr.size() == 5);
    static_assert(foo(arr) == 5);
    static_assert(bar(arr) == 5);
}

https://godbolt.org/z/q1xajW1Gq

However, if size is made static, msvc also compiles:
https://godbolt.org/z/Tfqe5E9z8

The problem seems to be with arr being passed by reference, as passing by value also get accepted.

Metadata

Metadata

Assignees

No one assigned

    Labels

    clang:frontendLanguage frontend issues, e.g. anything involving "Sema"constexprAnything related to constant evaluationduplicateResolved as duplicate

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions