Drake
drake_assert.h File Reference

Detailed Description

Provides Drake's assertion implementation.

This is intended to be used both within Drake and by other software. Drake's asserts can be armed and disarmed independently from the system-wide asserts.

#include <type_traits>
Include dependency graph for drake_assert.h:

Macros

#define DRAKE_ASSERT(condition)
 DRAKE_ASSERT(condition) is similar to the built-in assert(condition) from the C++ system header <cassert>. More...
 
#define DRAKE_ASSERT_VOID(expression)
 Like DRAKE_ASSERT, except that the expression must be void-valued; this allows for guarding expensive assertion-checking subroutines using the same macros as stand-alone assertions. More...
 
#define DRAKE_DEMAND(condition)
 Evaluates condition and iff the value is false will trigger an assertion failure with a message showing at least the condition text, function name, file, and line. More...
 
#define DRAKE_UNREACHABLE()
 Silences a "no return value" compiler warning by calling a function that always raises an exception or aborts (i.e., a function marked noreturn). More...
 

Macro Definition Documentation

◆ DRAKE_ASSERT

#define DRAKE_ASSERT (   condition)

DRAKE_ASSERT(condition) is similar to the built-in assert(condition) from the C++ system header <cassert>.

Unless Drake's assertions are disarmed by the pre-processor definitions listed below, DRAKE_ASSERT will evaluate condition and iff the value is false will trigger an assertion failure with a message showing at least the condition text, function name, file, and line.

By default, assertion failures will :abort() the program. However, when using the pydrake python bindings, assertion failures will instead throw a C++ exception that causes a python SystemExit exception.

Assertions are enabled or disabled using the following pre-processor macros:

  • If DRAKE_ENABLE_ASSERTS is defined, then DRAKE_ASSERT is armed.
  • If DRAKE_DISABLE_ASSERTS is defined, then DRAKE_ASSERT is disarmed.
  • If both macros are defined, then it is a compile-time error.
  • If neither are defined, then NDEBUG governs assertions as usual.

This header will define exactly one of either DRAKE_ASSERT_IS_ARMED or DRAKE_ASSERT_IS_DISARMED to indicate whether DRAKE_ASSERT is armed.

This header will define both constexpr bool drake::kDrakeAssertIsArmed and constexpr bool drake::kDrakeAssertIsDisarmed globals.

One difference versus the standard assert(condition) is that the condition within DRAKE_ASSERT is always syntax-checked, even if Drake's assertions are disarmed.

Treat DRAKE_ASSERT like a statement – it must always be used in block scope, and must always be followed by a semicolon.

◆ DRAKE_ASSERT_VOID

#define DRAKE_ASSERT_VOID (   expression)

Like DRAKE_ASSERT, except that the expression must be void-valued; this allows for guarding expensive assertion-checking subroutines using the same macros as stand-alone assertions.

◆ DRAKE_DEMAND

#define DRAKE_DEMAND (   condition)

Evaluates condition and iff the value is false will trigger an assertion failure with a message showing at least the condition text, function name, file, and line.

◆ DRAKE_UNREACHABLE

#define DRAKE_UNREACHABLE ( )

Silences a "no return value" compiler warning by calling a function that always raises an exception or aborts (i.e., a function marked noreturn).

Only use this macro at a point where (1) a point in the code is truly unreachable, (2) the fact that it's unreachable is knowable from only reading the function itself (and not, e.g., some larger design invariant), and (3) there is a compiler warning if this macro were removed. The most common valid use is with a switch-case-return block where all cases are accounted for but the enclosing function is supposed to return a value. Do not use this macro as a "logic error" assertion; it should only be used to silence false positive warnings. When in doubt, throw an exception manually instead of using this macro.