assert is a macro to do run time debugging checks.
In debug mode, all assert will run.
In release mode, the asserts are ignored. Or, more precise, by #defining NDEBUG, the preprocessor removes all assertss from your code. assert is #defined in cassert.h
- assert example 1: basics
- assert example 2: basics with informative output
- assert example 3: a user defined assert
- Assert in a C++ Builder console Application
- Assert in a Qt Creator console Application
- Use assert extensively [1-5,7]
- The use of assert statements can help to document the assumptions you make when implementing your code [6]
- Do not assume that assert is always evaluated [8]
- Never put code with side effects inside an assert [9]
- [1] Herb Sutter, Andrei Alexandrescu. C++ coding standards: 101 rules, guidelines, and best practices. ISBN: 0-32-111358-6. Chapter 68: 'Assert liberally to document internal assumptions and invariants'
- [2] Bjarne Stroustrup. The C++ Programming Language (3rd edition). 1997. ISBN: 0-201-88954-4. Advice 24.5.18: 'Explicitly express preconditions, postconditions, and other assertions as assertions'
- [3] Steve McConnell. Code Complete (2nd edition). 2004. ISBN: -735619670. Chapter 8.2 'Assertions', paragraph 'Guidelines for using asserts': 'Use assertions to document and verify preconditions and postconditions'
- [4] Steve McConnell. Code Complete (2nd edition). 2004. ISBN: -735619670. Chapter 8.2 'Assertions', paragraph 'Guidelines for using asserts': 'Use assertions for conditions that should never occur'.
- [5] Jesse Liberty. Sams teach yourself C++ in 24 hours. ISBN: 0-672-32224-2. Hour 24, chapter 'assert()': 'Use assert freely'
- [6] John Lakos. Large-Scale C++ Software Design. 1996. ISBN: 0-201-63362-0. Chapter 2.6: 'The use of assert statements can help to document the assumptions you make when implementing your code
- [7] Bjarne Stroustrup. The C++ Programming Language (4th edition). 2013. ISBN: 978-0-321-56384-2. Chapter 30.5. Advice. page 884: '[13] Use static_assert() and assert() extensively'
- [8] Bjarne Stroustrup. The C++ Programming Language (4th edition). 2013. ISBN: 978-0-321-56384-2. Chapter 30.5. Advice. page 884: '[14] Do not assume that assert() is always evaluated'
- [9] Jason Turner, cppbestpractices: Never Put Code with Side Effects Inside an assert()