Skip to content

OpenVINO™ is an open source toolkit for optimizing and deploying AI inference

[core] Fix as_memory_view to take a const reference for improved safety (#39246)

Pawel Raasz70f1dc2dcc62
diff --git a/src/common/util/include/openvino/util/memory.hpp b/src/common/util/include/openvino/util/memory.hpp
index efe7b214b91..6086b2099af 100644
--- a/src/common/util/include/openvino/util/memory.hpp
+++ b/src/common/util/include/openvino/util/memory.hpp
@@ -170,12 +170,12 @@ private:
/** @brief Non-owning byte view of @p value's object representation; @p value must outlive the view. */
template <typename T>
-MemoryView as_memory_view(T& value) noexcept {
+MemoryView as_memory_view(const T& value) noexcept {
static_assert(std::is_trivially_copyable_v<T>, "as_memory_view requires a trivially copyable type");
- return {reinterpret_cast<const std::byte*>(&value), sizeof(T)};
+ return {reinterpret_cast<const std::byte*>(std::addressof(value)), sizeof(T)};
}
-template <typename T>
+template <typename T, std::enable_if_t<!std::is_lvalue_reference_v<T>>* = nullptr>
MemoryView as_memory_view(T&&) = delete;
/**