xenium
aligned_object.hpp
1 //
2 // Copyright (c) 2018-2020 Manuel Pöter.
3 // Licensed under the MIT License. See LICENSE file in the project root for full license information.
4 //
5 
6 #ifndef XENIUM_ALIGNED_OBJECT_HPP
7 #define XENIUM_ALIGNED_OBJECT_HPP
8 
9 #include <cstddef>
10 #include <type_traits>
11 
12 namespace xenium {
13 
27 template <typename Derived, std::size_t Alignment = 0>
29  static void* operator new(size_t sz) { return ::operator new(sz, alignment()); }
30 
31  static void operator delete(void* p) { ::operator delete(p, alignment()); }
32 
33 private:
34  static constexpr std::align_val_t alignment() {
35  return static_cast<std::align_val_t>(Alignment == 0 ? std::alignment_of<Derived>() : Alignment);
36  }
37 };
38 } // namespace xenium
39 
40 #endif
xenium::aligned_object
A small helper class for correctly aligned dynamic allocations of over-aligned types.
Definition: aligned_object.hpp:28