From 0b400af9519444deef4cc6ad2c43c30e2092ab4f Mon Sep 17 00:00:00 2001 From: Dominick Allen Date: Sat, 4 Jan 2025 09:56:12 -0600 Subject: Fix bug related to string copying. --- include/fud_option.hpp | 17 +++++++++++++---- 1 file changed, 13 insertions(+), 4 deletions(-) (limited to 'include/fud_option.hpp') diff --git a/include/fud_option.hpp b/include/fud_option.hpp index 3c94eaa..9d3068c 100644 --- a/include/fud_option.hpp +++ b/include/fud_option.hpp @@ -75,10 +75,11 @@ class Option { private: static_assert(!std::is_same_v); static constexpr bool IsRef = std::is_reference_v; - using ValueType = typename std::remove_reference::type; - static constexpr size_t Size = IsRef ? sizeof(std::reference_wrapper) : sizeof(ValueType); public: + using ValueType = std::remove_reference_t; + static constexpr size_t Size = IsRef ? sizeof(std::reference_wrapper) : sizeof(ValueType); + constexpr Option() noexcept : m_engaged{false} { } @@ -100,6 +101,7 @@ class Option { constexpr static Option take(T&& value) noexcept { Option option{}; + option.m_engaged = true; if constexpr (IsRef) { new (option.m_data.data()) std::reference_wrapper(std::ref(value)); } else { @@ -152,7 +154,8 @@ class Option { return !m_engaged; } - operator bool() const { + operator bool() const + { return hasValue(); } @@ -160,8 +163,10 @@ class Option { { fudAssert(m_engaged); if constexpr (IsRef) { + // NOLINTNEXTLINE(cppcoreguidelines-pro-type-reinterpret-cast) return *reinterpret_cast*>(m_data.data()); } else { + // NOLINTNEXTLINE(cppcoreguidelines-pro-type-reinterpret-cast) return *reinterpret_cast(m_data.data()); } } @@ -170,8 +175,10 @@ class Option { { fudAssert(m_engaged); if constexpr (IsRef) { + // NOLINTNEXTLINE(cppcoreguidelines-pro-type-reinterpret-cast) return *reinterpret_cast*>(m_data.data()); } else { + // NOLINTNEXTLINE(cppcoreguidelines-pro-type-reinterpret-cast) return *reinterpret_cast(m_data.data()); } } @@ -180,6 +187,7 @@ class Option { { fudAssert(m_engaged); static_assert(!IsRef); + // NOLINTNEXTLINE(cppcoreguidelines-pro-type-reinterpret-cast) return std::move(*reinterpret_cast(m_data.data())); } @@ -200,7 +208,7 @@ class Option { } template - constexpr auto map(F&& func) const & -> Option(func)(value()))> + constexpr auto map(F&& func) const& -> Option(func)(value()))> { using U = decltype(std::forward(func)(value())); // static_assert(std::is_same_v(func)(value())), Option>()); @@ -217,6 +225,7 @@ class Option { if constexpr (IsRef) { // reinterpret_cast*>(m_data.data()); } else { + // NOLINTNEXTLINE(cppcoreguidelines-pro-type-reinterpret-cast) reinterpret_cast(m_data.data())->~ValueType(); } cleanup(); -- cgit v1.2.3