Search code examples
c++assertfoldconstantfolding

Folding away assertions in C++ class?


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)


Solution

  • In the class example you provided, there's no way for the compiler to assume the constant is zero because you have two runtime variables:

    • Your const int val_ is only constant for each instance of the class so it can never optimise the class's function code since it must cater for every case.
    • The example instantiation doesn't provide a literal constant, it provides the result of rand() which is variable. It may be possible for it to optimise it out if it knows the ONLY val_ ever being provided to all instances of that class is zero.

    Have you tried providing a constant to the constructor to see if it optimises it out?