Is assert(false) ignored in release mode?

后端 未结 6 1816
耶瑟儿~
耶瑟儿~ 2020-12-15 04:12

I am using VC++. Is assert(false) ignored in release mode?

相关标签:
6条回答
  • 2020-12-15 04:51

    I think it is a mistake to rely too much on the exact behavior of the assert. The correct semantics of "assert(expr)" are:

    • The expression expr may or may not be evaluated.
    • If expr is true, execution continues normally.
    • If expr is false, what happens is undefined.

    More at http://nedbatchelder.com/text/assert.html

    0 讨论(0)
  • 2020-12-15 04:54

    IIRC, assert(x) is a macro that evaluates to nothing when NDEBUG is defined, which is the standard for Release builds in Visual Studio.

    0 讨论(0)
  • 2020-12-15 04:54

    The assert macro (at least it is typically a macro) is usually defined to no-op in release code. It will only trigger in debug code. Having said that. I have worked at places which defined their own assert macro, and it triggered in both debug and release mode.

    I was taught to use asserts for condition which can "never" be false, such as the pre-conditions for a function.

    0 讨论(0)
  • 2020-12-15 05:06

    Only if NDEBUG is defined I think (which it will be by default for Visual C++ apps).

    0 讨论(0)
  • 2020-12-15 05:08

    same for GNU :

      #ifdef    NDEBUG
    
      # define assert(expr)     (__ASSERT_VOID_CAST (0))
    
    0 讨论(0)
  • 2020-12-15 05:11

    If compiling in release mode includes defining NDEBUG, then yes.

    See assert (CRT)

    0 讨论(0)
提交回复
热议问题