Skip to content

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

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
StefanPaulet opened this issue Nov 29, 2024 · 2 comments
Labels
clang:frontend Language frontend issues, e.g. anything involving "Sema" constexpr Anything related to constant evaluation duplicate Resolved as duplicate

Comments

@StefanPaulet
Copy link

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.

@github-actions github-actions bot added the clang Clang issues not falling into any other category label Nov 29, 2024
@tbaederr tbaederr added the constexpr Anything related to constant evaluation label Nov 29, 2024
@frederick-vs-ja frederick-vs-ja added the duplicate Resolved as duplicate label Nov 29, 2024
@frederick-vs-ja
Copy link
Contributor

This is being fixed by #95474, which implements P2280R4.

Closing as duplicate of #63139.

@frederick-vs-ja frederick-vs-ja closed this as not planned Won't fix, can't repro, duplicate, stale Nov 29, 2024
@EugeneZelenko EugeneZelenko added clang:frontend Language frontend issues, e.g. anything involving "Sema" and removed clang Clang issues not falling into any other category labels Nov 29, 2024
@llvmbot
Copy link
Member

llvmbot commented Nov 29, 2024

@llvm/issue-subscribers-clang-frontend

Author: None (StefanPaulet)

The following code compiles on gcc, but fails on msvc and clang: ``` C++ 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.
</details>

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
clang:frontend Language frontend issues, e.g. anything involving "Sema" constexpr Anything related to constant evaluation duplicate Resolved as duplicate
Projects
None yet
Development

No branches or pull requests

5 participants