2015-06-22 39 views

回答

1

是的,這是可能的。 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; 
     ^

確實注意到然而,這僅適用於在此報關是可見的代碼。你可能想檢查你使用的任何庫的代碼,包括標準庫。