Add custom messages in assert?

后端 未结 8 988
心在旅途
心在旅途 2020-12-04 07:18

Is there a way to add or edit the message thrown by assert? I\'d like to use something like

assert(a == b, \"A must be equal to B\");

Then

8条回答
  •  离开以前
    2020-12-04 07:27

    As zneak's answer convolutes the code somewhat, a better approach is to merely comment the string text you're talking about. ie.:

    assert(a == b); // A must be equal to B
    

    Since the reader of the assert error will look up the file and line anyway from the error message, they will see the full explanation here.

    Because, at the end of the day, this:

    assert(number_of_frames != 0); // Has frames to update
    

    reads better than this:

    assert(number_of_frames != 0 && "Has frames to update");
    

    in terms of human parsing of code ie. readability. Also not a language hack.

提交回复
热议问题