在assert中添加自定义消息? [英] Add custom messages in assert?

查看:236
本文介绍了在assert中添加自定义消息?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

有没有办法添加或编辑由assert抛出的消息?我想使用像

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, the compiler adds line, time and so on...

有可能吗?

推荐答案

我遇到的一个黑客是使用&& 运算符。因为指针是真,如果它不是null,你可以做以下,而不改变条件:

A hack I've seen around is to use the && operator. Since a pointer "is true" if it's non-null, you can do the following without altering the condition:

assert(a == b && "A is not equal to B");

由于 assert 它也会显示您的消息。如果还不够,您可以编写自己的 myAssert 函数或宏,它将显示您想要的任何内容。

Since assert shows the condition that failed, it will display your message too. If it's not enough, you can write your own myAssert function or macro that will display whatever you want.

这篇关于在assert中添加自定义消息?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆