So, in a non-class type of situation, I can do something like this:
int val_to_check = 0;
int some_func(int param) {
assert(val_to_check == 0);
return param*param+param;
}
int main() {
printf("Val: %i\n", some_func(rand()));
return 0;
}
If val_to_check
is declared const
instead, the assertion can be folded away by the compiler.
I'm curious if it's possible to get a similar constant folding with a member variable of a class. For example, can I do something like:
class Test {
public:
Test(int val) : val_(val) {}
int some_func(int param) {
assert(val_ == 0);
return param*param+param;
}
private:
const int val_;
};
So val_ must be known when the class is defined, a-la:
Test my_test(0);
printf("Val: %i\n", my_test.some_func(rand()));
(I know these are contrived examples). It seems like it should be possible to fold the assertions away sometimes, but the simple examples I've tested don't seem to do it. The best I've gotten is moving the assertion code to the end of the function (when compiling w/ -O3)