0
在編譯時或者至少在使用pc-lint進行靜態分析時,是否可以強制使用運算符new
嚴格使用std::nothrow
?使用C++(GCC)4.8.3 20140911(Red Hat 4.8.3-9)編譯器。在編譯時或在靜態分析過程中執行std :: nothrow
在編譯時或者至少在使用pc-lint進行靜態分析時,是否可以強制使用運算符new
嚴格使用std::nothrow
?使用C++(GCC)4.8.3 20140911(Red Hat 4.8.3-9)編譯器。在編譯時或在靜態分析過程中執行std :: nothrow
是的,這是可能的。 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; ^
確實注意到然而,這僅適用於在此報關是可見的代碼。你可能想檢查你使用的任何庫的代碼,包括標準庫。