diff options
author | Steven Le Rouzic <steven.lerouzic@gmail.com> | 2025-03-26 18:54:49 +0100 |
---|---|---|
committer | Steven Le Rouzic <steven.lerouzic@gmail.com> | 2025-03-26 18:54:49 +0100 |
commit | 2c457c42758f1aeb5e2ec15f89b9702586328220 (patch) | |
tree | b437aee4dd40c09e9959f6ad17df687fa7b2751a | |
parent | 95598e24e16ddd23a5d425e3a797f7518a93c324 (diff) |
Add address_of
-rw-r--r-- | asl/memory/memory.hpp | 11 | ||||
-rw-r--r-- | asl/types/BUILD.bazel | 1 | ||||
-rw-r--r-- | asl/types/function_ref.hpp | 5 | ||||
-rw-r--r-- | todo.txt | 2 |
4 files changed, 16 insertions, 3 deletions
diff --git a/asl/memory/memory.hpp b/asl/memory/memory.hpp index 2c5301e..5ea4766 100644 --- a/asl/memory/memory.hpp +++ b/asl/memory/memory.hpp @@ -17,6 +17,17 @@ constexpr void* operator new(size_t, void* ptr) noexcept namespace asl { +template<typename T> +[[nodiscard]] +constexpr T* address_of(T& obj) +{ + return __builtin_addressof(obj); +} + +template<typename T> +void address_of(const T&& obj) = delete; + +[[nodiscard]] constexpr isize_t memcmp(const void* a, const void* b, isize_t size) { return __builtin_memcmp(a, b, static_cast<size_t>(size)); diff --git a/asl/types/BUILD.bazel b/asl/types/BUILD.bazel index 198d0a2..53905a3 100644 --- a/asl/types/BUILD.bazel +++ b/asl/types/BUILD.bazel @@ -97,6 +97,7 @@ cc_library( ], deps = [ "//asl/base", + "//asl/memory", ], visibility = ["//visibility:public"], ) diff --git a/asl/types/function_ref.hpp b/asl/types/function_ref.hpp index 8daa5bd..8d874f1 100644 --- a/asl/types/function_ref.hpp +++ b/asl/types/function_ref.hpp @@ -7,6 +7,7 @@ #include "asl/base/utility.hpp" #include "asl/base/meta.hpp" #include "asl/base/functional.hpp" +#include "asl/memory/memory.hpp" namespace asl { @@ -43,7 +44,7 @@ public: && same_as<invoke_result_t<T, Args...>, R> ) // NOLINTNEXTLINE(*cast*) - : m_obj{const_cast<void*>(reinterpret_cast<const void*>(&t))} + : m_obj{const_cast<void*>(reinterpret_cast<const void*>(address_of(t)))} , m_invoke{invoke<un_ref_t<T>>} {} @@ -56,7 +57,7 @@ public: ) { // NOLINTNEXTLINE(*cast*) - m_obj = const_cast<void*>(reinterpret_cast<const void*>(&t)); + m_obj = const_cast<void*>(reinterpret_cast<const void*>(address_of(t))); m_invoke = invoke<un_ref_t<T>>; return *this; @@ -1 +1 @@ - +Separate memory and allocator |