-2

In C++, new can mean three things (if there are more, please tell me) :

  • int * ptr = new int; -> allocation + construction
  • int * ptr = (int *) operator new(sizeof(int)) -> allocation only
  • int x; new (&x) int; -> construction only (used with objects)

How do I distinguish between those when speaking, verbally?

HolyBlackCat
  • 78,603
  • 9
  • 131
  • 207
  • 2
    *How can we easily tell the difference orally* -- I have no idea what you mean by "orally". – PaulMcKenzie Aug 29 '20 at 10:52
  • A good way would be to read what you wrote. – anastaciu Aug 29 '20 at 10:55
  • @PaulMcKenzie verbally – linternetsansfil Aug 29 '20 at 11:05
  • You mean, what name is used to designate each thing in your list? – StoryTeller - Unslander Monica Aug 29 '20 at 11:07
  • @StoryTeller-UnslanderMonica yes. – linternetsansfil Aug 29 '20 at 11:08
  • 1
    New expression, new operator and placement new expression. Usually when you say "new" you mean new expression. The other two cases are rare and people generally refer to them by their full name. This queation is pretty clear and I don't think it should be closed. – François Andrieux Aug 29 '20 at 11:29
  • @FrançoisAndrieux If I heard "new operator", I'd probably think about `new T` ([cppreference](https://en.cppreference.com/w/cpp/language/operator_precedence) lists it as an operator). I think I'd go for something like "the allocation function named `operator new`". – HolyBlackCat Aug 29 '20 at 11:32
  • @HolyBlackCat Although `new T` eventually leads to `operator new` being called, it is not an operator call in its self. The list you linked refers to the `operator new`s which is distinct from a `new` expression. `new operator` is the correct terminology and doesn't designate `new T`. – François Andrieux Aug 29 '20 at 11:37
  • @FrançoisAndrieux The link calls it `new new[]`, so it has to refer to the new-expression. I'd argue that `operator new` is a function with a funny name, rather than an operator. – HolyBlackCat Aug 29 '20 at 11:45
  • Here's a similar question on how to address the terminology as used in Java. Common terms are (arguably, conceptually in order, with some temporal overlap): declaration, allocation, instantiation, initialization, construction https://stackoverflow.com/questions/15074083/difference-between-initializing-a-class-and-instantiating-an-object – michael Aug 29 '20 at 11:53
  • @HolyBlackCat The confusion may come from the fact that new expressions (`new T`) are sometimes called "new operator" (distinct from "operator new" by swapping the order "operator" and "new"). I don't believe "new operator" exists in the standard and that it is just a misuse of the term "operator". The correct terminology is "new expression" for `new T` and "operator new" for `operator new`. – François Andrieux Aug 29 '20 at 12:19
  • @HolyBlackCat If you still disagree, we can continue this in the comments section of my answer. – François Andrieux Aug 29 '20 at 12:26
  • @FrançoisAndrieux No, I wasn't really trying to argue if `new T` is an operator or not. I meant that it's often *called* an operator (possibly incorrectly), so if you say "operator new", some people will assume you're talking about `new T` rather than `operator new`. – HolyBlackCat Aug 29 '20 at 12:43
  • @HolyBlackCat Thanks, now I understand what you were saying. I agree with that sentiment. – François Andrieux Aug 29 '20 at 12:45

2 Answers2

3
  • int * ptr = new int;

This is called "new expression".

  • int * ptr = (int *) operator new(sizeof(int))

This is called operator new. When used to allocate arrays it is called operator new[].

  • int x; new (&x) int;

This is also a "new expression" but with an optional placement argument being provided. To distinguish it from a "new expression" without a placement argument it is usually referred to as "placement new".

Because non-placement new expressions are the most common, they are usually what is referred to when the term "new" is used in C++.

François Andrieux
  • 28,148
  • 6
  • 56
  • 87
  • so there are acctually only two forms – linternetsansfil Aug 29 '20 at 13:11
  • I'm not sure what you mean by "form". The three expressions do very different things, so I guess you could consider them "forms". The language recognizes two things called new; new expressions and new operators. But there are two cases ("forms"?) of new expressions, twice that if you consider new expressions that create arrays to be different and something like 30 different overloads ("forms"?) of `operator new` and `operator new[]`. It depends on what you mean. – François Andrieux Aug 29 '20 at 13:18
  • @linternetsansfil -- "new expression" is part of the language, just as `struct`, `switch`, etc. `operator new` is part of the library. (It's a bit more complicated, because the compiler inherently knows about `operator new`, but the implementation of `operator new` is in the standard library) – Pete Becker Aug 29 '20 at 14:45
0

Maybe "allocation-and-construction new", "allocation-only new" and "construction-only new", the last one is also named "placement new".