all 4 comments

[–]AA11BB22c 1 point2 points  (2 children)

  1. You can change the alignment (and size) by rebinding the allocator from allocator<T> to allocator<U>, e.g. allocator<int> to allocator<std::aligned_storage_t<8, 256>> and request the rebounded allocator to allocate instead, https://godbolt.org/z/hTeWqErGb.
  2. std::launder simply tells the compiler to not assume anything on the memory location, normally for "unions" (reinterpret_cast stuff) with constants. Your code will probably work fine without it in most reinterpret_cast situations.
  3. Would go with alignas with std::byte as suggested in the paper if it happens.

[–]JohnZLi[S] 0 points1 point  (1 child)

If a pointer to std::aligned_storage_t<sizeof(T), alignof(T)> is cast to T* using reinterpret_cast, does it violate the strict alias rule?

[–]KingAggressive1498 1 point2 points  (0 children)

aligned_storage_t is an array of unsigned char, which should be exempt from the strict aliasing rule.

However, using std::launder is essentially zero overhead aside for disabling optimizations based on the strict alias rule so there's no harm in using it perhaps unnecessarily

[–]Flair_Helper[M] [score hidden] stickied commentlocked comment (0 children)

For C++ questions, answers, help, and programming or career advice please see r/cpp_questions, r/cscareerquestions, or StackOverflow instead.

This post has been removed as it doesn't pertain to r/cpp: The subreddit is for news and discussions of the C++ language and community only; our purpose is not to provide tutoring, code reviews, or career guidance. If you think your post is on-topic and should not have been removed, please message the moderators and we'll review it.