Skip to content

Instantly share code, notes, and snippets.

@pervognsen
Created October 5, 2014 08:35
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save pervognsen/d1c7b3a183aa6352164c to your computer and use it in GitHub Desktop.
Save pervognsen/d1c7b3a183aa6352164c to your computer and use it in GitHub Desktop.
In new(p) T(x) the C++ standard requires that placement new performs a null pointer check on p.
Unfortunately, this can add a pointless conditional to inner loops that use dynamic arrays implemented
in C++ via placement new. I don't expect the optimizer to perform a global analysis to conclude that p
can never be null, so I have to help it on its way. However, none of these three tricks convince Clang 3.5:
new(&*p) T(x);
(void) *p;
new(p) T(x);
if (!p) __builtin_unreachable();
new(p) T(x);
Does anyone have a better idea?
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment