Came across this need a while back and was forced to work around it. I'm wondering if there is a way to carry state across a compile time check.
So for example a motivating example would be how to set up counter so that you can do something like:
static_assert(foo() == 0, "..");
static_assert(foo() == 1, "..");
static_assert(foo() == 2, "..");
Where each subsequent call increased the result by 1. I'm specifically interested in accomplishing this at compile time. I've tried setting up foo as a constexpr with an internal counter but then hit read-only constraints. I would like to know whether something along these lines is possible with the current C++ standard.
int main() {
static constexpr int counter = 0;
struct test
{
constexpr int foo(){return counter++;}
};
test myTest;
static_assert(myTest.foo() == 0, "failed");
static_assert(myTest.foo() == 1, "failed");
return 0;
}
This is not feasible at compile time:
myTest
are instantiated at runtime. But it's easy at runtime, with normal variables and asserts.