static_assert can be used to prohibit the use of the delete keyword as follows:
#define delete static_assert(0, "The keyword \"delete\" is forbidden.");
Every modern C ++ developer might want to do this if he or she wants to use a conservative garbage collector, using only the es class and struct , which overload the new operator to call a function that allocates memory in the conservative heap of a conservative garbage collector that can be initialized and created, by calling some function that does this at the beginning of the main function.
For example, every modern C ++ developer who wants to use the Boehm-Demers-Weiser conservative garbage collector will write at the beginning of the main function:
GC_init();
And in each class and struct overload operator new as follows:
void* operator new(size_t size) { return GC_malloc(size); }
And now that operator delete no longer needed, since the Boehm-Demers-Weiser conservative garbage collector is responsible for both freeing up and freeing every block of memory when it is no longer needed, the developer wants to disable delete keyword.
One way is to overload the delete operator this way:
void operator delete(void* ptr) { assert(0); }
But this is not recommended because a modern C ++ developer will know that he / she mistakenly called delete operator at run time, but it is better to know this soon at compile time.
Therefore, in my opinion, the best solution to this scenario is to use static_assert , as shown at the beginning of this answer.
Of course, this can also be done with BOOST_STATIC_ASSERT , but I think static_assert better and should always be preferred.
user11962338 Aug 23 '19 at 22:45 2019-08-23 22:45
source share