Errors and Assertions

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

Ryan McCombe
Updated
Hello! I'm here to help you with any questions about this lesson. Feel free to ask me anything - whether it's about specific concepts, examples, or how to apply what you've learned!

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?
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?
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?