Adding message to assert

2019-01-21 16:38发布

Hallo!

I'm looking for a way to add custom messages to assert statements. I found this questions Add custom messages in assert? but the message is static there. I want to do something like this:

assert((0 < x) && (x < 10), std::string("x was ") + myToString(x));

When the assertion fails I want the normal output plus for example "x was 100".

7条回答
霸刀☆藐视天下
2楼-- · 2019-01-21 17:15

There are some old tricks to include messages without writing your own routines:

The first is this:

bool testbool = false;
assert(("this is the time", testbool));

There is also:

bool testbool = false;
assert(testbool && "This is a message");

The first one works, because the inside parens expression result is the value of 'testbool'. The second one works, because the value of the string is going to be non-zero.

查看更多
登录 后发表回答