Why is assert a macro and not a function?

前端 未结 5 822
爱一瞬间的悲伤
爱一瞬间的悲伤 2020-12-23 16:06

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

5条回答
  •  予麋鹿
    予麋鹿 (楼主)
    2020-12-23 16:38

    This macro is disabled if, at the moment of including , a macro with the name NDEBUG has already been defined. This allows for a coder to include as many assert calls as needed in a source code while debugging the program and then disable all of them for the production version by simply including a line like:

    #define NDEBUG 
    

    at the beginning of its code, before the inclusion of .

    Therefore, this macro is designed to capture programming errors, not user or run-time errors, since it is generally disabled after a program exits its debugging phase.


    Making it as function will increase some function calls and you can not control all such asserts in release mode.

    If you use function then _FILE__, __LINE__ and __func__ will give the value of that assert function's code. Not that calling line or calling function's line.

提交回复
热议问题