assert() with message

前端 未结 7 726
醉梦人生
醉梦人生 2020-12-24 01:08

I saw somewhere assert used with a message in the following way:

assert((\"message\", condition));

This seems to work great, except that gc

7条回答
  •  予麋鹿
    予麋鹿 (楼主)
    2020-12-24 01:46

    Try:

    #define assert__(x) for ( ; !(x) ; assert(x) )
    

    use as such:

    assert__(x) {
        printf("assertion will fail\n"); 
    }
    

    Will execute the block only when assert fails.

    IMPORTANT NOTE: This method will evaluate expression x twice, in case x evaluates to false! (First time, when the for loop is checking its condition; second time, when the assert is evaluating the passed expression!)

提交回复
热议问题