Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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.

like image 396
François Andrieux Avatar asked Oct 20 '20 15:10

François Andrieux


People also ask

Can we use the new operator on int to create an int object?

Since arrays are object in java, hence while instantiating arrays, we use new operator. For example: int arr[] = new int[5];

What is operator new?

The new operator lets developers create an instance of a user-defined object type or of one of the built-in object types that has a constructor function.

What does New int do in C?

*new int means "allocate memory for an int , resulting in a pointer to that memory, then dereference the pointer, yielding the (uninitialized) int itself".


4 Answers

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.

like image 89
Sneftel Avatar answered Oct 22 '22 00:10

Sneftel


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 is operator new and the deallocation function's name is operator delete. If the allocated type is an array type, the allocation function's name is operator new[] and the deallocation function's name is operator 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.

like image 43
Useless Avatar answered Oct 22 '22 01:10

Useless


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 +.

like image 12
t.niese Avatar answered Oct 22 '22 01:10

t.niese


This is governed by [expr.new], which clearly differentiates between a new-expression and how a new expression may obtain storage by means of calling an allocation function, which is named operator new. Particularly, from [expr.new]/8 [emphasis mine]:

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 is operator new and the deallocation function's name is operator delete. If the allocated type is an array type, the allocation function's name is operator new[] and the deallocation function's name is operator delete[].


Is it correct to say that the keyword new as it is used in a new expression is an operator?

Particularly, the example, albeit non-normative, of [expr.new]/4 describe this function as an operator function; "[...] the new operator":

[...] Instead, the explicitly parenthesized version of the new operator can be used [...]

like image 9
dfrib Avatar answered Oct 22 '22 01:10

dfrib