Is it possible to enforce the use of operator new strictly with std::nothrow at compile time or at least during static analysis using pc-lint? Using c++ (GCC) 4.8.3 20140911 (Red Hat 4.8.3-9) compiler.
enforce std::nothrow at compile time or during static analysis
212 views Asked by krizajb At
1
Yes, it's possible. GCC supports the
errorattribute, which makes any use of a particular function a hard error. Applying this tooperator newhas the expected effect.This is rejected by the compiler with:
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; ^Do note however that this applies only to code in which this custom declaration is visible. You may want to check the code of any libraries you use, including the standard library.