Consider security software in which dynamic allocation is generally prohibited and exceptions are prohibited. Dynamic allocation is only allowed if class explication defines the new and delete operator. Using the new operator for another class should cause compilation to fail.
The easiest way to cause compilation to fail in the described situation is to delete the global new statement:
void* operator new(std::size_t) = delete;
On the one hand, it causes side effects with the standard library. For example, the inclusion of <array> applies to <new_allocator> to <stdexcept> . <new_allocator> uses the ::new operator, and this reason for the failure to fail even when you do not want to use memory allocation and allocation. According to Scoot Meyers, <array> should be bare metal.
On the other hand, this causes an error with the built-in compiler operator
src/main.cpp:91:31: error: deleted definition of 'void* operator new(std::size_t)' void* operator new(std::size_t) = delete; ^ <built-in>: note: previous declaration of 'void* operator new(std::size_t)'
Is there a solution to ban ::new and use <array> ?
Is there any solution to ban ::new globally at all ?
source share