What is the “assert” function?

后端 未结 9 2250
耶瑟儿~
耶瑟儿~ 2020-11-27 09:16

I\'ve been studying OpenCV tutorials and came across the assert function; what does it do?

9条回答
  •  广开言路
    2020-11-27 09:25

    C++11 N3337 standard draft

    http://www.open-std.org/jtc1/sc22/wg21/docs/papers/2012/n3337.pdf

    19.3 Assertions

    1 The header , described in (Table 42), provides a macro for documenting C ++ program assertions and a mechanism for disabling the assertion checks.

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

    C99 N1256 standard draft

    http://www.open-std.org/JTC1/SC22/WG14/www/docs/n1256.pdf

    7.2 Diagnostics

    1 The header defines the assert macro and refers to another macro, NDEBUG which is not defined by . 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.

    2. 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.

    7.2.1 Program diagnostics

    7.2.1.1 The assert macro

    Synopsis

    1.

    #include 
    void assert(scalar expression);
    

    Description

    2 The assert macro puts diagnostic tests into programs; it expands to a void expression. When it is executed, if expression (which shall have a scalar type) is false (that is, compares equal to 0), the assert macro writes information about the particular call that failed (including the text of the argument, the name of the source file, the source line number, and the name of the enclosing function — 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.

    Returns

    3 The assert macro returns no value.

提交回复
热议问题