如何将断言宏实现为方法?
我想将断言宏实现为 C++ 中的方法,例如 .NET Framewrk。
例如,在 C# 中,我们可以像这样调用断言方法:
Debug.Assert(index > -1);
我想实现这样的断言:
#include <assert.h>
class debug
{
public:
static void my_asset(<condition>) // like assert macro
{
// ?
}
};
使用此类时:
debug::my_asset(index > -1); // Actually should be called assert(index > -1);
谢谢
编辑:
我想在调用 debug::my_asset(index >) 时; -1);
,它显示正确的文件名和行号,其工作方式类似于C++资源宏。
I want implement assert macro as a method in C++ like .NET Framewrk.
For example in C# we can invoke assert method like this:
Debug.Assert(index > -1);
and I want implement assert something like this:
#include <assert.h>
class debug
{
public:
static void my_asset(<condition>) // like assert macro
{
// ?
}
};
When using this class:
debug::my_asset(index > -1); // Actually should be called assert(index > -1);
Thanks
Edit:
I want when invoking debug::my_asset(index > -1);
, It shows correct file name and line number, and it works like C++ asset macro.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
断言
assert
(在
或
中)有几个令人感兴趣的功能如果不使用宏,则无法在 C++ 中执行这些操作。
There are several features of assert
assert
(in<assert.h>
or<cassert>
) that are of interest:You can't do these in C++ without using a macro.
获取行号、文件和文本版本的唯一方法是通过宏。然而:
这段代码的工作方式很奇怪。第一个 x 是断言表达式本身,计算结果为 true 或 false,以告诉 my_assert 要做什么。
#x
是一个神奇的预处理器命令,它生成x
的 char* 版本,以便我们可以显示它。__FILE__
替换为文件名,__LINE__
替换为行号。 (由于它是一个宏,因此它与调用函数具有相同的行号)。当您输入
debug::myassert(sizeof(int)==4);
时,预处理器会说“我知道myassert(whatever)
是什么!”并替换它。因此它将全部替换为:debug::my_assert(sizeof(int)==4, "sizeof(int)==4", "main.cpp", 27);
,这是有效的代码行。因此,例如,如果 sizeof(int) 为 8(在某些机器上),则第一个参数为 false,并且该行显示“failed assert sizeof(int)==4 in main.cpp” 27岁。”The only way to get the line number, file, and text version is via macro. However:
This code works in an oddball way. The first
x
is the assert expression itself, evaluating to true or false, to tell my_assert what to do. The#x
is a magic preprocessor command that makes a char* version ofx
, so we can display it.__FILE__
is replaced with the filename, and__LINE__
is replaced with the line number. (Since it's a macro, it has the same line number as the calling function).When you type
debug::myassert(sizeof(int)==4);
, the preprocessor says "I know whatmyassert(whatever)
is!" and replaces it. So it replaces it all with:debug::my_assert(sizeof(int)==4, "sizeof(int)==4", "main.cpp", 27);
, which is a valid line of code. So if sizeof(int) is 8 for instance (it is on some machines), the first parameter isfalse
, and the line is displayed "failed assert sizeof(int)==4 in main.cpp at 27."不起作用?
Doesn't work?
你可以
这样做,因为当断言失败时它会导致异常。
它比宏的帮助要小一些,因为
my_assert
无法看到失败的条件是什么——你会得到一个断言失败,但没有有用的解释(尽管你会得到准确的堆栈跟踪)在调试器中)。另请参阅 Ken 的为什么宏更有帮助的原因。
You could
This would work in that it would cause an exception when the assertion failed.
It's a little less helpful than the macro because
my_assert
can't see what the condition was that failed -- you'll get an assertion failure but no useful explanation (though you'll get an accurate stack trace in a debugger).Also see Ken's reasons why a macro is more helpful.