在 VC++ 中找不到 ascii 字符串的 _assert()

Cannot find _assert() for ascii string in VC++

根据 MSDN documentation for assert 宏,它支持 _assert() [ASCII 字符串] 和 _wassert() [UNICODE 字符串]。但是在 Windows 套件文件夹中的 assert.h 文件中,它定义如下:

 #define assert(expression) (void)(                                                       \
            (!!(expression)) ||                                                              \
            (_wassert(_CRT_WIDE(#expression), _CRT_WIDE(__FILE__), (unsigned)(__LINE__)), 0) \
        )

我的项目需要断言的 ASCII 版本。是否有为 _assert() 定义的任何其他头文件?

在 MSDN 文档页面上,它说:

The signature of the _assert function is not available in a header file.

所以你的问题的答案是:"No"。此外,MSDN 页面告诉您:

The _assert and _wassert functions are internal CRT functions. They help minimize the code required in your object files to support assertions. We do not recommend that you call these functions directly.

如果您想打印一条 ASCII 错误消息,然后死掉,我建议使用 MessageBox 后跟 ExitProcess(对于 GUI 应用程序)或 fputs 后跟 abort(对于控制台应用程序)。

似乎没有 header 为 ascii string 定义 assert(),即使用 _assert() 方法。因此,为了满足我的需要,我使用以下代码创建了一个本地 assert.h 文件,当使用它时,它会编译并链接到正确的 ascii 函数。

#undef assert

#ifdef NDEBUG

#define assert(expression) ((void)0)

#else

    void __cdecl _assert(
        _In_z_ const* _Message,
        _In_z_ const* _File,
        _In_   unsigned       _Line
        );

#define assert(exp) (void)( (exp) || (_assert(#exp, __FILE__, __LINE__), 0) )

#endif

以上当然是内部assert.hheader代码的启发