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

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

问题描述

有没有办法添加或编辑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");

然后,编译器添加linetime等等...

Then, the compiler adds line, time and so on...

有可能吗?

推荐答案

我见过的一个技巧是使用 && 操作符.由于指针在非空时为真",您可以在不改变条件的情况下执行以下操作:

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.

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

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