添加消息以断言
我正在寻找一种将自定义消息添加到断言语句的方法。 我发现这个问题 Add custom messages in assert? 但该消息是静态的。我想做这样的事情:
assert((0 < x) && (x < 10), std::string("x was ") + myToString(x));
当断言失败时,我想要正常输出加上例如“x was 100”。
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".
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(9)
你在这里运气不好。最好的方法是定义您自己的
assert
宏。基本上,它看起来像这样:
仅当未定义非调试宏
NDEBUG
时,才会定义ASSERT
宏。然后你会像这样使用它:
这比你的用法简单一点,因为你不需要显式地字符串化
"x was " 和
x
,这就完成了由宏隐式地实现。You are out of luck here. The best way is to define your own
assert
macro.Basically, it can look like this:
This will define the
ASSERT
macro only if the no-debug macroNDEBUG
isn’t defined.Then you’d use it like this:
Which is a bit simpler than your usage since you don’t need to stringify
"x was "
andx
explicitly, this is done implicitly by the macro.有一些老技巧可以在不编写自己的例程的情况下包含消息:
第一个是这样的:
还有:
第一个有效,因为内部括号表达式结果是“testbool”的值。
第二个有效,因为字符串的值将非零。
There are some old tricks to include messages without writing your own routines:
The first is this:
There is also:
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.
更好的选择是教调试器在失败时停止断言,然后您不仅可以检查 x 值,还可以检查包括调用堆栈在内的任何其他信息。或许,这才是你真正想要的。
这里提到了示例实现 在使用 C++ 编程时向您的合作程序员显示某些方法尚未在类中实现的方法
A better alternative is to teach the debugger to stop on assert when it fails, then you could examine not only the x value but any other information including call stack. Perhaps, this is what you are really looking for.
Sample implementation is mentioned here Ways to show your co-programmers that some methods are not yet implemented in a class when programming in C++
扩展 Kondrad Rudolph 的答案:
输出是...
这类似于 std::assert 宏在我的系统上输出的内容,只是带有附加的用户定义消息
Extending on Kondrad Rudolph's answer:
Output is...
which is similar to what the std::assert macro outputs on my system just with the additional user defined message
为了完整起见,我在 C++ 中发布了一个包含 2 个文件的断言宏实现:
将提示您:
其中
在程序的剩余执行中忽略它
abort()
(on视窗,系统将提示用户附加调试器)
abort()
您可以在此处找到更多信息:
希望有帮助。
For the sake of completeness, I published a drop-in 2 files assert macro implementation in C++:
Will prompt you with:
Where
ignore it for the remaining execution of the program
abort()
(on Windows,the system will prompt the user to attach a debugger)
abort()
immediatelyYou can find out more about it there:
Hope that helps.
是的,这是可能的。
要启用像
better_assert((0 < x) && (x < 10), std::string("x was ") + myToString(x));
这样的表达式,我们是应该有一个相应的宏,其形式为
print_assertion
是执行断言的代理函数。当EXPRESSION
的值为false
时,所有调试信息__VA_ARGS__
将被转储到std::cerr
>。该函数接受任意数量的参数,因此我们应该实现一个可变参数模板函数:在之前的实现中,表达式
(out << ... << args) << std::endl;
在 C++17 中使用折叠表达式 ( https://en.cppreference.com/w/cpp/language/fold);常量表达式debug_mode
与传递的编译选项有关,可以定义为 还值得一提的是,表达式
if constexpr( debug_mode )
使用了 constexpr if ( https://en.cppreference.com/w/cpp/language/if) 自 C++17 起导入。为了总结一切,我们有:
一个典型的测试用例演示其用法可以是:
这将产生如下错误消息:
完整的源代码可在此存储库中找到: https://github.com/fengwang/better_assert
Yes, this is possible.
To enable expression like
better_assert((0 < x) && (x < 10), std::string("x was ") + myToString(x));
, we are supposed to have a corresponding macro in a form ofin which
print_assertion
is a proxy function to do the assertion. When theEXPRESSION
is evaluatedfalse
, all the debug information, the__VA_ARGS__
, will be dumped tostd::cerr
. This function takes arbitrary numbers of arguments, thus we should implement a variadic templated function:In the previous implementation, the expression
(out << ... << args) << std::endl;
make use of fold expression in C++17 (https://en.cppreference.com/w/cpp/language/fold); the constant expressiondebug_mode
is related to the compilation options passed, which is can be defined asIt also worth mentioning that the expression
if constexpr( debug_mode )
makes use of constexpr if (https://en.cppreference.com/w/cpp/language/if) imported since C++17.To wrap everything up, we have:
A typical test case demonstrating its usage can be:
This will produce something error message like:
And the full source code is available in this repo: https://github.com/fengwang/better_assert
为了接受 Feng Wang 的回答,在较新版本的 C++ 中,任何内联内容都将被优化掉。因此,您可以在头文件中使用内联函数来完成所有工作。
一些评论:
如果未定义
_DEBUG
,该函数将变为空,因此可以 100% 优化如果调用具有副作用的函数,非调试代码仍然有效:
这些副作用在这里清晰可见。但是,如果您调用一个函数,则很难知道该函数中是否发生了意外的事情。
不过,有一些方法可以防止此类副作用的发生(示例)。但总而言之,在某些情况下,您需要调用函数进行测试,这可能会产生副作用,因此不需要在非调试代码中进行优化。
我使用
abort()
因为我知道可以正确停止调试器,std::terminate()
是现代系统中执行相同操作的 C++ 方式,但如果std::terminate()
不适用于您的调试器,则abort()
将会。NOT_USED()
是为了避免有关未使用的函数参数的警告(如果没有该警告,则可能会出现预期的错误)。我在 snapdev 中有一个实现:请参阅
safe_assert。 h
和not_used.h
。To take on Feng Wang answer, in newer versions of C++, anything inline is going to be optimized out. So you can have an inline function in a header file which does all the work.
Some comments:
If
_DEBUG
is not defined, the function becomes empty so it can be optimized out 100%If you call the function with a side effect, the non-debug code still works:
These side effects are clearly visible here. However, if you call a function, it could be really difficult to know whether something happens in the function which was not otherwise expected.
There are ways to prevent such side effects from happening, though (Example). But all in all, in some cases, you need to call a function for the test and it may have a side effect and therefore needs to not be optimized out in non-debug code.
I use
abort()
because I know that stops the debugger properly,std::terminate()
is the C++ way which in modern systems does the same thing, but ifstd::terminate()
doesn't work for your debugger theabort()
will.The
NOT_USED()
is to avoid warnings about unused function parameters (if you don't have that warning, you may end up with expected bugs).I have an implementation in snapdev: see
safe_assert.h
andnot_used.h
.沿着康拉德鲁道夫的答案,你可以做得更简洁一点
,它也适用于C,
它使用你链接的问题的一些答案的一般思想,但宏允许它更灵活一点
going along with Konrad Rudolf's answer you can do it a bit more concise with
which also works in C,
it works using the general idea from some of the answers to the question you linked, but the macro allows it to be a little more flexible