我正在尝试替换 Visual Studio 和 C++ 中的全局 new 运算符。这是我的代码(为简单起见,仅显示了一个新运算符):
void* operator new(size_t _Size)
{
// Do something
}
它工作正常,但是在为我的项目运行代码分析时,Visual Studio 给了我一个警告:
warning C28251: Inconsistent annotation for 'new': this instance has no annotations. The first user-provided annotation on this built-in function is at line vcruntime_new.h(48).
按照 IntelliSense 警告中的建议,使用 vcruntime_new.h 中的注释进行operator new
替换,可以解决警告:
_NODISCARD _Ret_notnull_ _Post_writable_byte_size_(_Size) _VCRT_ALLOCATOR
void* __cdecl operator new(size_t _Size)
{
// Do something
}
- 将 vcruntime_new.h 中的注释用于我自己的替换代码是否安全,如上所示?
- 这种变化的后果是什么?
- 是否有特殊用例是因为注释而不能像以前一样使用“新运算符”?
- 为什么这种改变是必要的?
编辑:
- 我是否正确,注释不会改变生成的二进制文件中的任何内容,并且只是用于静态代码分析?(除了
__cdecl
,它改变了汇编器,但我猜它应该是标准的?)