My understanding was that the parameters of consteval functions can't be used in static_assert
s because they are not considered constant expressions. However, that only seems to be the case sometimes:
// using gcc 13.1 with -std=c++20
#include <array>
#include <concepts>
#include <iostream>
#include <ranges>
template<std::ranges::input_range T, std::integral U>
consteval int assertStuff2(T ok, U not_ok) {
// (1) both compile fine
static_assert(std::ranges::size(ok) != 5);
static_assert(ok.size() != 5);
// (2) compiler error!
// "non-constant condition for static assertion:
// 'not_ok' is not a constant expression"
static_assert(not_ok != 5);
return 0;
}
int main() {
std::cout << assertStuff2(std::array<int, 3>{5, 3, 2}, 2);
}
Why is ok
usable in a static_assert
but not_ok
isn't?