作者热门文章
- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
是否有可能在编译时或至少在使用 pc-lint 的静态分析期间强制使用 std::nothrow
严格使用运算符 new
?使用 c++ (GCC) 4.8.3 20140911 (Red Hat 4.8.3-9) 编译器。
最佳答案
是的,这是可能的。 GCC 支持 error
属性,这使得对特定函数的任何使用都成为硬错误。将此应用于 operator new
具有预期的效果。
#include <cstddef>
void *operator new(std::size_t) __attribute__((error("use new(std::nothrow) instead")));
int main() {
new int;
}
这被编译器拒绝了:
h.cc: In function ‘int main()’:h.cc:6:10: error: call to ‘operator new’ declared with attribute error: use new(std::nothrow) instead new int; ^
但是请注意,这仅适用于此自定义声明可见的代码。您可能想要检查您使用的任何库的代码,包括标准库。
关于c++ - 在编译时或静态分析期间强制执行 std::nothrow,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30974523/
我是一名优秀的程序员,十分优秀!