Search code examples
c++new-operatordelete-operatornew-expression

C++ primer 5th edition: operator new and operator delete overloading


can someone explain to me this paragraph from C++ primer 5th edition:

Terminology: new Expression versus operator new Function

The library functions operator new and operator delete are misleadingly named. Unlike other operator functions, such as operator=, these functions do not overload the new or delete expressions. In fact, we cannot redefine the behavior of the new and delete expressions.

A new expression always executes by calling an operator new function to obtain memory and then constructing an object in that memory. A delete expression always executes by destroying an object and then calling an operator delete function to free the memory used by the object.

By providing our own definitions of the operator new and operator delete functions, we can change how memory is allocated. However, we cannot change this basic meaning of the new and delete operators.

I don't see a difference between operator new or operator delete and any other overloaded operator like the assignment operator =. So what it means "are misleadingly named"? and we all know that we don't override an expression like fObj + fObj but we overload the operator not the expression itself.

In fact I find this paragraph itself misleading. After all we can "abuse" any overloadable operator and from which operator new and delete so what did he mean in this paragraph? Thank you!


Solution

  • Most operators in C++ aren't bound by any explicit semantics or requirements. The only real exceptions are operator new and operator delete due to its specialized use, which is what this is referring to.

    For example, lets consider operator==.

    Even though it's conventional (and wise) to have this operator perform some kind of comparison and return a bool to indicate equality -- this actually isn't required by the C++ language.

    In fact, it's actually entirely possible to define operator== to return something completely unrelated -- perhaps an int, a std::string, or something weirder like std::tuple. And, of course, it need not actually perform any comparison.

    Effectively, the semantics of most operators in C++ are weakly required by convention, but not by the language.

    This contrasts with operator new and operator delete. A new expression in C++ will always start a dynamic object's lifetime at the pointer returned by the operator new invocation. Whether this is new (p) T{...} for a placement-new expression, new T{...} with the global-new operator, or some new(args,...) T{...} for a custom operator new -- it must return some form of pointer to start a lifetime at for T.

    Similarly, delete must end that lifetime, and call operator delete to release the underlying storage for that lifetime. This effectively forces semantics of operator new and operator delete to perform some form of allocation mechanism, and some form of cleanup mechanism respectively. It's not really possible to define new and delete to do something strange (as in the case of operator==), since the implied behavior of calling these operators will simply break (if it compiles at all).

    This is why the quote mentions that the behavior of operator new/operator delete cannot be redefined; the basic meaning will always be fixed.