Search code examples
cassertc-preprocessor

Why is assert a macro and not a function?


My lecturer has asked me that in class, and I was wondering why is it a macro instead of a function?


Solution

  • The simple explanation would be that the standard requires assert to be a macro, if we look at the draft C99 standard(as far as I can tell the sections are the same in draft C11 standard as well) section 7.2 Diagnostics paragraph 2 says:

    The assert macro shall be implemented as a macro, not as an actual function. If the macro definition is suppressed in order to access an actual function, the behavior is undefined.

    Why does it require this, the rationale given in Rationale for International Standard—Programming Languages—C is:

    It can be difficult or impossible to make assert a true function, so it is restricted to macro form.

    which is not very informative, but we can see from other requirements why. Going back to section 7.2 paragraph 1 says:

    [...]If NDEBUG is defined as a macro name at the point in the source file where is included, the assert macro is defined simply as

    #define assert(ignore) ((void)0)
    

    The assert macro is redefined according to the current state of NDEBUG each time that is included.

    This is important since it allows us an easy way to turn off assertions in release mode where you may want to take the cost of potentially expensive checks.

    and the second important requirement is that it is required to use the macros __FILE__, __LINE__ and __func__, which is covered in section 7.2.1.1 The assert macro which says:

    [...] the assert macro writes information about the particular call that failed [...] the latter are respectively the values of the preprocessing macros __FILE_ _ and __LINE_ _ and of the identifier __func_ _) on the standard error stream in an implementation-defined format.165) It then calls the abort function.

    where footnote 165 says:

    The message written might be of the form:

    Assertion failed: expression, function abc, file xyz, line nnn.
    

    Having it as a macro allows the macros __FILE__ etc... to be evaluated in the proper location and as Joachim points out being a macro allows it to insert the original expression in the message it generates.

    The draft C++ standard requires that the contents of the cassert header are the same as the assert.h header from Standrd C library:

    The contents are the same as the Standard C library header .

    See also: ISO C 7.2.

    Why (void)0?

    Why use (void)0 as opposed to some other expression that does nothing? We can come up with a few reasons, first this is how the assert synopsis looks in section 7.2.1.1:

    void assert(scalar expression);
    

    and it says (emphasis mine):

    The assert macro puts diagnostic tests into programs; it expands to a void expression.

    the expression (void)0 is consistent with the need to end up with a void expression.

    Assuming we did not have that requirement, other possible expressions could have undesirable effects such as allowing uses of assert in release mode that would not be allowed in debug mode for example using plain 0 would allow us to use assert in an assignment and when used correctly would likely generate an expression result unused warning. As for using a compound statement as a comment suggests, we can see from C multi-line macro: do/while(0) vs scope block that they an have undesirable effects in some cases.