Skip to content

Call to consteval method from constexpr constructor makes constructor consteval. #123052

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
IViktorov opened this issue Jan 15, 2025 · 4 comments
Closed
Labels
clang:frontend Language frontend issues, e.g. anything involving "Sema" consteval C++20 consteval constexpr Anything related to constant evaluation

Comments

@IViktorov
Copy link

#include <cstdint>
#include <print>



template <class T, std::size_t kSize>
struct Test {
    constexpr Test(const Test& t) noexcept: m_data{t.data()}, m_size{t.size()} {}
    constexpr Test(T (&data)[kSize]) noexcept: m_data{data}, m_size{kSize} {}

    constexpr T* data() const noexcept { return m_data; }
#ifdef NOCONSTEVAL
    constexpr static std::size_t size() noexcept { return kSize; }
#else
    consteval static std::size_t size() noexcept { return kSize; }
#endif

private:
    T* m_data;
    std::size_t m_size;
};



int main() {
    int arr[] = {1, 2, 3, 4, 5, 6};
    Test t1{arr};
    Test t{t1};
    std::println("arr[0] = {}, arr size = {}.", *t.data(), t.size());
}

With consteval size() method Test copy constructor becomes immediate function, but it should not.
Without consteval with constexpr size() everything goes as expected.
GCC doesn't have that problem with consteval methods, called in contructors.

https://godbolt.org/z/P157TafW8

@tbaederr
Copy link
Contributor

CC @cor3ntin

@EugeneZelenko EugeneZelenko added clang:frontend Language frontend issues, e.g. anything involving "Sema" consteval C++20 consteval constexpr Anything related to constant evaluation and removed new issue labels Jan 15, 2025
@llvmbot
Copy link
Member

llvmbot commented Jan 15, 2025

@llvm/issue-subscribers-clang-frontend

Author: None (IViktorov)

```C++ #include <cstdint> #include <print>

template <class T, std::size_t kSize>
struct Test {
constexpr Test(const Test& t) noexcept: m_data{t.data()}, m_size{t.size()} {}
constexpr Test(T (&data)[kSize]) noexcept: m_data{data}, m_size{kSize} {}

constexpr T* data() const noexcept { return m_data; }

#ifdef NOCONSTEVAL
constexpr static std::size_t size() noexcept { return kSize; }
#else
consteval static std::size_t size() noexcept { return kSize; }
#endif

private:
T* m_data;
std::size_t m_size;
};

int main() {
int arr[] = {1, 2, 3, 4, 5, 6};
Test t1{arr};
Test t{t1};
std::println("arr[0] = {}, arr size = {}.", *t.data(), t.size());
}


With `consteval size()` method `Test` copy constructor becomes immediate function, but it should not.
Without consteval with `constexpr size()` everything goes as expected.
GCC doesn't have that problem with consteval methods, called in contructors.

https://godbolt.org/z/P157TafW8
</details>

@cor3ntin
Copy link
Contributor

Note that if the call to size doesn't try to read this, clang is perfectly happy
https://godbolt.org/z/171P8erfa

Maybe yet another instance of #63139 @shafik

@cor3ntin
Copy link
Contributor

Fixed by #95474

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" consteval C++20 consteval constexpr Anything related to constant evaluation
Projects
None yet
Development

No branches or pull requests

5 participants