如何将断言宏实现为方法?

发布于 2024-12-01 09:56:10 字数 575 浏览 2 评论 0原文

我想将断言宏实现为 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

荭秂 2024-12-08 09:56:10

断言 assert(在 中)有几个令人感兴趣的功能

  • :正在编译发布版本,根本不评估断言测试。
  • 断言失败时打印的错误消息可以告诉您断言失败的文件和行号,
  • 它还可以告诉您失败的确切代码。

如果不使用宏,则无法在 C++ 中执行这些操作。

There are several features of assert assert (in <assert.h> or <cassert>) that are of interest:

  • that when you're compiling a release build, the assert tests aren't evaluated at all.
  • the error message printed when an assertion fails can tell you the file and line number where your assertion failed
  • it can also tell you the exact code that failed.

You can't do these in C++ without using a macro.

一个人的旅程 2024-12-08 09:56:10

获取行号、文件和文本版本的唯一方法是通过宏。然而:

#include <assert.h>
class debug
{
public:
    static void my_assert(bool passed, const char* assert, const char* file, long line)
    {
        if (passed == false)
            std::cout<<"failed assert "<<assert<<" in "<<file<<" at "<<line<<".\n";
    }
#ifdef NDEBUG
    #define myassert(x) my_assert(true, "", "", 0)
#else 
    #define myassert(x) my_assert(x, #x , __FILE__, __LINE__ )
#endif
};


int main() {
    debug::myassert(sizeof(int)==4);
    return 0,
}

这段代码的工作方式很奇怪。第一个 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:

#include <assert.h>
class debug
{
public:
    static void my_assert(bool passed, const char* assert, const char* file, long line)
    {
        if (passed == false)
            std::cout<<"failed assert "<<assert<<" in "<<file<<" at "<<line<<".\n";
    }
#ifdef NDEBUG
    #define myassert(x) my_assert(true, "", "", 0)
#else 
    #define myassert(x) my_assert(x, #x , __FILE__, __LINE__ )
#endif
};


int main() {
    debug::myassert(sizeof(int)==4);
    return 0,
}

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 of x, 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 what myassert(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 is false, and the line is displayed "failed assert sizeof(int)==4 in main.cpp at 27."

橘虞初梦 2024-12-08 09:56:10
static void my_asset(bool cond) // like assert macro
{
  ::assert(cond);
}

不起作用?

static void my_asset(bool cond) // like assert macro
{
  ::assert(cond);
}

Doesn't work?

予囚 2024-12-08 09:56:10

你可以

void debug::my_assert(bool cond) {
    ASSERT(cond);
}

这样做,因为当断言失败时它会导致异常。

它比宏的帮助要小一些,因为 my_assert 无法看到失败的条件是什么——你会得到一个断言失败,但没有有用的解释(尽管你会得到准确的堆栈跟踪)在调试器中)。

另请参阅 Ken 的为什么宏更有帮助的原因。

You could

void debug::my_assert(bool cond) {
    ASSERT(cond);
}

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文