struct test {
int var;
consteval test(int i) : var{i}
{
static_assert(i == 3);
}
};
int main()
{
constexpr test t{3};
}
This is rejected with:
$ g++ c.cpp -std=c++20
c.cpp: In constructor ‘consteval test::test(int)’:
c.cpp:8:25: error: non-constant condition for static assertion
8 | static_assert(i == 3);
| ~~^~~~
c.cpp:8:23: error: ‘i’ is not a constant expression
8 | static_assert(i == 3);
| ^
Why can't I use static_assert if the constructor is guaranteed to be evaluated at compile time ?