Mistake on this page?
Report an issue in GitHub or email us
Macros | Functions
Assert macros

Macros

#define MBED_ASSERT(expr)
 MBED_ASSERT Declare runtime assertions: results in runtime error if condition is false. More...
 
#define MBED_STATIC_ASSERT(expr, msg)   enum {MBED_CONCAT(MBED_ASSERTION_AT_, __LINE__) = sizeof(char[(expr) ? 1 : -1])}
 MBED_STATIC_ASSERT Declare compile-time assertions, results in compile-time error if condition is false. More...
 
#define MBED_STRUCT_STATIC_ASSERT(expr, msg)   bool : (expr) ? 0 : -1
 MBED_STRUCT_STATIC_ASSERT Declare compile-time assertions, results in compile-time error if condition is false. More...
 

Functions

MBED_NORETURN void mbed_assert_internal (const char *expr, const char *file, int line)
 Internal mbed assert function which is invoked when MBED_ASSERT macro fails. More...
 

Detailed Description

Macro Definition Documentation

#define MBED_ASSERT (   expr)
Value:
do { \
if (!(expr)) { \
mbed_assert_internal(#expr, __FILE__, __LINE__); \
} \
} while (0)

MBED_ASSERT Declare runtime assertions: results in runtime error if condition is false.

Note
Use of MBED_ASSERT is limited to Debug and Develop builds.
1 int Configure(serial_t *obj) {
2  MBED_ASSERT(obj);
3 }

Definition at line 65 of file mbed_assert.h.

#define MBED_STATIC_ASSERT (   expr,
  msg 
)    enum {MBED_CONCAT(MBED_ASSERTION_AT_, __LINE__) = sizeof(char[(expr) ? 1 : -1])}

MBED_STATIC_ASSERT Declare compile-time assertions, results in compile-time error if condition is false.

The assertion acts as a declaration that can be placed at file scope, in a code block (except after a label), or as a member of a C++ class/struct/union.

Note
Use of MBED_STATIC_ASSERT as a member of a struct/union is limited:
  • In C++, MBED_STATIC_ASSERT is valid in class/struct/union scope.
  • In C, MBED_STATIC_ASSERT is not valid in struct/union scope, and MBED_STRUCT_STATIC_ASSERT is provided as an alternative that is valid in C and C++ class/struct/union scope.
1 MBED_STATIC_ASSERT(MBED_LIBRARY_VERSION >= 120,
2  "The mbed library must be at least version 120");
3 
4 int main() {
5  MBED_STATIC_ASSERT(sizeof(int) >= sizeof(char),
6  "An int must be larger than a char");
7 }

Definition at line 110 of file mbed_assert.h.

#define MBED_STRUCT_STATIC_ASSERT (   expr,
  msg 
)    bool : (expr) ? 0 : -1

MBED_STRUCT_STATIC_ASSERT Declare compile-time assertions, results in compile-time error if condition is false.

Unlike MBED_STATIC_ASSERT, MBED_STRUCT_STATIC_ASSERT can and must be used as a member of a C/C++ class/struct/union.

1 struct thing {
2  MBED_STATIC_ASSERT(2 + 2 == 4,
3  "Hopefully the universe is mathematically consistent");
4 };

Definition at line 133 of file mbed_assert.h.

Function Documentation

MBED_NORETURN void mbed_assert_internal ( const char *  expr,
const char *  file,
int  line 
)

Internal mbed assert function which is invoked when MBED_ASSERT macro fails.

This function is active only if NDEBUG is not defined prior to including this assert header file. In case of MBED_ASSERT failing condition, error() is called with the assertation message.

Parameters
exprExpression to be checked.
fileFile where assertation failed.
lineFailing assertation line number.
Important Information for this Arm website

This site uses cookies to store information on your computer. By continuing to use our site, you consent to our cookies. If you are not happy with the use of these cookies, please review our Cookie Policy to learn how they can be disabled. By disabling cookies, some features of the site will not work.