Assertion

suggest change

Introduction

An assertion is a predicate that the presented condition must be true at the moment the assertion is encountered by the software. Most common are simple assertions, which are validated at execution time. However, static assertions are checked at compile time.

Syntax

Parameters

Parameter | Details |

——— | —–– |

expression | expression of scalar type. |

message | string literal to be included in the diagnostic message. |

Remarks

Both assert and static_assert are macros defined in assert.h.

The definition of assert depends on the macro NDEBUG which is not defined by the standard library. If NDEBUG is defined, assert is a no-op:

#ifdef NDEBUG#  define assert(condition) ((void) 0)#else#  define assert(condition) /* implementation defined */#endif

Opinion varies about whether NDEBUG should always be used for production compilations.

static_assert expands to _Static_assert which is a keyword. The condition is checked at compile time, thus condition must be a constant expression. There is no need for this to be handled differently between development and production.

Feedback about page:

Feedback:
Optional: your email if you want me to get back to you:



Table Of Contents