Avoiding Side Effects in Assertions

Is it okay to use assert() with expressions that have side effects, like assert(++x > 0)? Or should assertions be side-effect free?

In general, it's best practice to avoid side effects in assertions. Assertions should be used to check invariants and preconditions, not to modify state or perform logic that affects program behavior.

Here's an example of an assertion with a side effect:

#include <cassert>

void risky_increment(int& x) {
  assert(++x > 0);
}

int main() {
  int a = 0;
  risky_increment(a);
  // What's the value of a here?
}

The problem with this is:

  1. It's not immediately clear that x is being modified by the assertion.
  2. The behavior of the function changes depending on whether assertions are enabled (e.g. in debug vs release builds).

Instead, perform any necessary modifications separately and then assert on the result:

#include <cassert>

void safe_increment(int& x) {
  ++x;
  assert(x > 0);
}

This makes the increment explicit and ensures the function behavior is consistent regardless of assertion settings.

A similar guideline applies to assertions in constructors or destructors. Avoid complex logic or side effects that could leave the object in an indeterminate state if an assertion fails:

class User {
  std::string name;
  int age;
public:
  User(std::string name, int age) : name(name), age(age) {
    assert(age >= 0);
  }
};

Here, if the age assertion fails, the User object will still be constructed with whatever name value was provided. It's cleaner to validate the inputs before performing any initialization.

In summary:

  • Keep assertions side-effect free when possible
  • Perform any complex validation or mutation logic outside of assertions
  • Be mindful of assertions in special member functions to avoid half-initialized or half-destructed states

Errors and Assertions

Learn how we can ensure that our application is in a valid state using compile-time and run-time assertions.

Questions & Answers

Answers are generated by AI models and may not have been reviewed. Be mindful when running any code on your device.

Using Assert in Release Builds
The lesson mentions that assert() calls are sometimes stripped out in release builds for performance. What if I want to keep some critical assertions enabled in release builds?
Using static_assert with Non-constexpr Expressions
Can static_assert() be used with expressions that are not known at compile-time, such as values read from a file or user input?
Assertions vs Exceptions
When should I use assertions vs throwing exceptions? They seem to serve similar purposes.
Defensive Programming with Assertions
How can I use assertions to practice "defensive programming" and make my code more robust?
Using static_assert in Template Code
The lesson shows an example of using static_assert() with std::is_floating_point to validate template parameters. What are some other common type traits I can use for template validation?
Or Ask your Own Question
Get an immediate answer to your specific question using our AI assistant