A Proposal to Cancel Overriding the DEFAULT operator new/delete

Motivation

I don't think we need to override the DEFAULT operator new/delete. This makes your code doesn't work well together 3rdparty code (if it also overrides them).

Why overriding the DEFAULT operator new/delete is bad?

Allowing to override the DEFAULT operator new/delete means the DEFAULT operator new/delete are special operators. When we have more than one implementations of an operator, the compiler/linker will report a redefining error. However, it allows there are two implementations of the DEFAULT operator new/delete (one of them is implemented as default). It sounds good. But, This gives a hint that there may have many implementations of the DEFAULT operator new/delete. When this happens, the compiler/linker doesn't know what should it do, and it reports a redefining error. The C++ programmers have to solve such accidents, especially when they use 3rdparty codes —— You know, there are many libraries overriding the DEFAULT operator new/delete. But unfortunately, they take a risk of conflict with each other.

Impact on the Standard

Does my proposal break existing code? Yes, it does. But the compilers of C++0x have a lot of ways to solve this. For example, the compilers can skip overriding operator new/delete and use the default implementation. And they can give a warning that reminds programmers to eliminate outdated codes.

Why do I think to cancel overriding the DEFAULT operator new/delete is possible? Because overriding the DEFAULT operator new/delete only change the implementation, not the semantic (the concept). If implementations changes the semantic of DEFAULT operator new/delete, they are bad code indeed.

Proposed Text

I suggest ISO C++ cancel this feature, just like we can't override operator+ of all C types. That is:

1. You CAN NOT override these operators:

void* operator new(size_t size);
void operator delete(void* p);

2. You CAN override DEFAULT operator new in non global namespace:

namespace foo
{
    void* operator new(size_t size) { ... }
    void operator delete(void* p) { ... }
};

And then you can use new/delete objects as the following:

int* intObj = foo::new int;
foo::delete intObj;

3. You CAN override NON-DEFAULT operator new. Such as:

void* operator new(size_t size, my_allocator& alloc);

4. There is no need to override a NON-DEFAULT operator delete. If you need to delete objects in special way, just use:

alloc.destroy(obj);
Unless otherwise stated, the content of this page is licensed under Creative Commons Attribution-ShareAlike 3.0 License