Placement new in std::aligned_storage?

The most paranoid way is

::new ((void *)::std::addressof(storage)) T(a, b, c);

Explanation:

  • ::std::addressof guards against overloaded unary operator& on storage, which is technically allowed by the standard. (Though no sane implementation would do it.) The ::std guards against any non-top-level namespaces (or classes) called std that might be in scope.
  • (void *) (which in this case is the equivalent of a static_cast) ensures that you call the placement operator new taking a void * rather than something else like decltype(storage) *.
  • ::new skips any class-specific placement operator news, ensuring that the call goes to the global one.

Together, this guarantees that the call goes to the library placement operator new taking a void *, and that the T is constructed at where storage is.

In most sane programs, though,

new (&storage) T(a,b,c);

should be sufficient.

Leave a Comment

Hata!: SQLSTATE[HY000] [1045] Access denied for user 'divattrend_liink'@'localhost' (using password: YES)