Is `new` in `new int;` considered an operator?
The expression new int;
such as in int * x = new int;
is a new expression. The term "new operator" seems to be used interchangeably with "new expression", for example in this question : Difference between 'new operator' and 'operator new'?
Is it correct to say that the keyword new
as it is used in a new expression is an operator? Why or why not?
If not, is there another justification for calling a new expression "new operator"?
I am having trouble finding an authoritative definition for what constitutes an operator.
I already understand the distinction between operator new which allocates memory for an object and "new expression" which may eventually call an operator new
.
Solution 1:
new
in new int
is not considered to be an operator. It is also not considered to be not an operator.
The C++ standard is really vague, and even inconsistent, about what constitutes an 'operator'. When listing operators (as defined during lexing and preprocessing), it lists them along with "punctuators" (things like (
), but never really gives any rules for punctuators in general. It lists new
as both a keyword and an operator. It lists sizeof
in the set of keywords but NOT in the set of operators, but later refers to it as an operator.
The takeaway here is that the C++ standards committee is not overly concerned with separating the lexical world into "operators" and "non-operators". This is because there isn't really any need to. There's no grammatical rules which apply to all operators or all non-operators. Important sets, like the set of overloadable operators, are given separately; grammatical rules for things like binary arithmetic expressions are given one at a time.
Basically, "operator" is not a category which is formally meaningful to the C++ language, so any categorical answer is going to be based on how it "feels". You can call it an operator if you like, but you can also call it a keyword (or a punctuator!) and the language standard will not disagree with you.
Solution 2:
Is it correct to say that the keyword
new
as it is used in a new-expression is an operator? Why or why not?
No. The new
in a new-expression is a keyword identifying a new-expression.
A new-expression calls an operator new
or operator new[]
to get storage. It also initializes that storage, and de-allocates it (with operator delete
or operator delete[]
) if the initialization throws.
There's a clear distinction, in that operator new
is only referred to as a overloadable user-replaceable function, and a new-expression does more than just call this function.
Reference: 7.6.2.8/10 [expr.new]
A new-expression may obtain storage for the object by calling an allocation function (
[basic.stc.dynamic.allocation]
). If the new-expression terminates by throwing an exception, it may release storage by calling a deallocation function. If the allocated type is a non-array type, the allocation function's name isoperator new
and the deallocation function's name isoperator delete
. If the allocated type is an array type, the allocation function's name isoperator new[]
and the deallocation function's name isoperator delete[]
.
Consider by way of counterexample, that we define both
T operator+(T, T);
void* T::operator new(std::size_t);
for some type T, then addition in either form:
T a, b;
T c = a + b;
T d = T::operator +(a, b);
is identical. The infix notation is just syntactic sugar for the operator call.
Allocation however, behaves very differently:
T *p = new T;
// does much more than
void *v = T::operator new(sizeof(T));
so it's not reasonable to call the new-expression syntactic sugar for a call to operator new
. Thus, the new
keyword isn't simply selecting the function to call. It can't be, or it would have to mention the operator delete
function that might also be called.
Solution 3:
I would only call it new expression to avoid confusion with void* operator new ( std::size_t count )
which only allocates memory as part of the process the new expression invoices (allocation memory, starting lifetime, calling constructor).
The problem with new
is that it does more than just calling the operator new
. Which is a bit confusing because for x + y
the +
only calls operator +
.