1

Background

I am cleaning up a legacy codebase by applying a coding guideline for the new statement.

There is code like auto x = new(ClassName); that I rewrite to auto x = new ClassName();. It's quite obvious that this is not a placement new and I don't need to think about it.

However, there's also code like auto x = new(ClassName)(argument); which looks much like a placement new. For now I blindly rewrote such code to auto x = new ClassName(argument); as well.

Question

Might there be a case where a real placement new like auto x = new(placement-params)(Type); is rewritten as auto x = new placement-params(Type); and it still compiles but changes the meaning of the program?

Thomas Weller
  • 55,411
  • 20
  • 125
  • 222
  • The parentheses in `new(ClassName)` are just like parentheses around any general expression. And in this case they are simply not needed. The expression `new(ClassName)` is exactly equivalent to `new ClassName`. From this it should be quite clear that `new(ClassName)(argument)` is exactly the same as `new ClassName(argument)`, which is what you already translated it as. – Some programmer dude Jul 05 '22 at 07:00
  • @Someprogrammerdude: I'm sorry to disagree. You can't simply add arbitrarily many parentheses in `new(A)` like you do for a calculation. These parentheses do not behave like in any expression. See https://en.cppreference.com/w/cpp/language/new and https://godbolt.org/z/KK7eqY9s4. Also, that wasn't my question. The question is about a similar rewrite for a real placement new. – Thomas Weller Jul 05 '22 at 07:06
  • @ThomasWeller see (1) here https://en.cppreference.com/w/cpp/language/new note the "extra" brackets. – Richard Critten Jul 05 '22 at 07:23
  • @Someprogrammerdude You comment reminds me of last weeks : C++ weekly. ;) – Pepijn Kramer Jul 05 '22 at 07:26
  • @RichardCritten: yes, I know that `new(ClassName)` is valid syntax, but that's not the question. The question is whether rewriting an actual placement by removing the parens may result in compilable code that does something else. – Thomas Weller Jul 05 '22 at 07:32
  • Without a concrete example I don't think we can help if you already know the syntax. – Richard Critten Jul 05 '22 at 07:40
  • @RichardCritten: if I knew an example, I needn't ask the question. – Thomas Weller Jul 05 '22 at 07:44
  • 1
    BTW: `auto x = new(ClassName)` is not the same as `auto x = new ClassName();` see https://stackoverflow.com/questions/620137/do-the-parentheses-after-the-type-name-make-a-difference-with-new – Klaus Jul 05 '22 at 08:06
  • Looking here https://en.cppreference.com/w/cpp/language/new , it seems like `new (A)` is a placement new when `A` is a value, but not when it is a type. – BoP Jul 05 '22 at 08:15

1 Answers1

2

placement-params is not a type, it is a value.

Consider this code with a placement new:

int* buf = new int;
int* a = new(buf)(int);

If we remove parenthesis around buf, the compiler can easily detect buf is not a type.

int* a = new buf(int); // compile error

Even if we create a type named buf, by the name lookup rules, the name in the inner scope is found first:

class buf {         // found second
    buf(int) {}
};

int main() {
    int *buf = new int;  // found first
    int* a = new buf(int); // compile error
    return 0;
}

According to this answer, when type and value are in a same scope, the value is found first. For example:

class buf {         // found second
    buf(int) {}
};
int *buf = new int;  // found first

int main() {
    int *a = new buf(int);  // compile error
    return 0;
}
VLL
  • 9,634
  • 1
  • 29
  • 54