What is the use or what is the reason for new() and delete() to be implemented as operators in c++ ? What are the advantages of making it an operator instead of a function?
The new
operator cannot be a function because it accepts a type as an argument. You cannot write new foo_type
as a function call, because foo_type
is not an expression that produces a value, but a type name.
The delete
operator could be a function that is overloaded for different pointer types, and an extra optional bool
argument for delete []
semantics. Why there is a delete
operator is probably for symmetry with its new
counterpart.
That said, template functions can take types as template arguments. But the new
and delete
operators historically precede templates.
Also, a function can be written which takes, instead of a type, a prototype instance of a type, from which an object is constructed: newobj(m_class(constructor_arg))
. This newobj
is overloaded for different types. It allocates space, copy constructs into it, and returns the object.
So in the end, the design reflects the tastes and whims of the designer.
The separation itself between operators and functions (and statements, declarations, and so on) is not strictly necessary in language design.