2

What are the use cases for new elision and new combining, added in C++14? (As per cppreference on new)?

And also:

  • Does limiting them to "replacable allocation functions" mean they only apply if operator new has been replaced? Or does it also apply the to the compiler/library supplied operator new?
  • Can you tell if it's been used, or if it could be used, with some if-constexpr-style code?

For new combining I can guess it would make allocate_shared and make_shared easier to write - if you could actually depend on it being used. (Or could detect at compile time whether it would be used in an if-constexpr.) But if you couldn't depend on it being used or detect it you'd have to write those functions the "old-fashioned" way I guess, with some kind of placement new and then just computing pointers to pieces of it.

For new elision, I'm not sure what the use case would be. Compiler discovers something is small enough to fit on the stack (and makes an assumption that the stack won't overflow!) plus discovers its lifetime is deterministic and scoped and so places it in some stack frame with an overlapping scope? What would an actual example of this kind of thing be that's in common use? Or any example in common use?

davidbak
  • 5,775
  • 3
  • 34
  • 50
  • It's a possible optimization, an internal implementation detail. It's only mentioned in the standard because a program can notice it, by replacing `operator new` and observing that calls to it don't correspond 1:1 to `new` expressions. In a sense, it's similar to permitting the compiler to elide copy constructor calls even when they have side effects. – Igor Tandetnik Jan 09 '21 at 05:01

0 Answers0