Is using assert() in C++ bad practice?

后端 未结 5 1650
滥情空心
滥情空心 2020-12-23 00:27

I tend to add lots of assertions to my C++ code to make debugging easier without affecting the performance of release builds. Now, assert is a pure C macro desi

5条回答
  •  谎友^
    谎友^ (楼主)
    2020-12-23 01:03

    Assertions can be used to verify internal implementation invariants, like internal state before or after execution of some method, etc. If assertion fails it really means the logic of the program is broken and you can't recover from this. In this case the best you can do is to break as soon as possible without passing exception to the user. What is really nice about assertions (at least on Linux) is that core dump is generated as a result of process termination and thus you can easily investigate the stack trace and variables. This is much more useful to understand the logic failure than exception message.

提交回复
热议问题