diff --git a/include/optional.h b/include/optional.h index 81c5946..f67dcaf 100644 --- a/include/optional.h +++ b/include/optional.h @@ -54,7 +54,10 @@ using optional_t = std::optional; optional& operator=(optional const& other) { - _value = nullptr; + if (this == &other) { + return *this; + } + reset(); if (other.has_value()) { _value = new T{other.value()}; } diff --git a/include/set.h b/include/set.h index f6ef8f4..6630b9d 100644 --- a/include/set.h +++ b/include/set.h @@ -1065,25 +1065,25 @@ namespace fcpp { } // Returns the begin iterator, useful for other standard library algorithms - [[nodiscard]] typename std::set::iterator begin() + [[nodiscard]] typename std::set::iterator begin() { return m_set.begin(); } // Returns the const begin iterator, useful for other standard library algorithms - [[nodiscard]] typename std::set::const_iterator begin() const + [[nodiscard]] typename std::set::const_iterator begin() const { return m_set.begin(); } // Returns the end iterator, useful for other standard library algorithms - [[nodiscard]] typename std::set::iterator end() + [[nodiscard]] typename std::set::iterator end() { return m_set.end(); } // Returns the const end iterator, useful for other standard library algorithms - [[nodiscard]] typename std::set::const_iterator end() const + [[nodiscard]] typename std::set::const_iterator end() const { return m_set.end(); } diff --git a/tests/optional_test.cc b/tests/optional_test.cc index a48dadf..a5b1899 100644 --- a/tests/optional_test.cc +++ b/tests/optional_test.cc @@ -108,3 +108,14 @@ TEST(OptionalTest, CopyConstructorNullTest) const optional_t v1(v2); EXPECT_FALSE(v1.has_value()); } + +TEST(OptionalTest, SelfAssignmentTest) +{ + optional_t v(5); + // Assign through an alias so the self-assignment is not diagnosed by the + // compiler but still exercises operator=(optional const&) with this == &other. + const optional_t& alias = v; + v = alias; + EXPECT_TRUE(v.has_value()); + EXPECT_EQ(5, v.value()); +}