Consider safety software, where dynamic allocation in general is disallowed and exceptions are disallowed. Dynamic allocation is allowed only when class explicity defines operator new
and delete
. Using operator new
for others class should cause compilation failure.
The simplest way to cause compilation failure in described situation is to remove global new operator:
void* operator new(std::size_t) = delete;
On the one hand this cause side effects with standard library. For example including <array>
propagates inclusion to <new_allocator>
by <stdexcept>
. <new_allocator>
uses ::new
operator and this cause build fail even when You don't want to use exception and memory allocation. According to Scoot Meyers <array>
should be bare metal friendly.
On the other hand this cause error with compiler built-in 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 any solution to ban ::new
and use <array>
?
Is there any solution to ban ::new
globally at all?
Aucun commentaire:
Enregistrer un commentaire