Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 4 additions & 1 deletion include/optional.h
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,10 @@ using optional_t = std::optional<T>;

optional& operator=(optional const& other)
{
_value = nullptr;
if (this == &other) {
return *this;
}
reset();
if (other.has_value()) {
_value = new T{other.value()};
}
Expand Down
8 changes: 4 additions & 4 deletions include/set.h
Original file line number Diff line number Diff line change
Expand Up @@ -1065,25 +1065,25 @@ namespace fcpp {
}

// Returns the begin iterator, useful for other standard library algorithms
[[nodiscard]] typename std::set<TKey>::iterator begin()
[[nodiscard]] typename std::set<TKey, TCompare>::iterator begin()
{
return m_set.begin();
}

// Returns the const begin iterator, useful for other standard library algorithms
[[nodiscard]] typename std::set<TKey>::const_iterator begin() const
[[nodiscard]] typename std::set<TKey, TCompare>::const_iterator begin() const
{
return m_set.begin();
}

// Returns the end iterator, useful for other standard library algorithms
[[nodiscard]] typename std::set<TKey>::iterator end()
[[nodiscard]] typename std::set<TKey, TCompare>::iterator end()
{
return m_set.end();
}

// Returns the const end iterator, useful for other standard library algorithms
[[nodiscard]] typename std::set<TKey>::const_iterator end() const
[[nodiscard]] typename std::set<TKey, TCompare>::const_iterator end() const
{
return m_set.end();
}
Expand Down
11 changes: 11 additions & 0 deletions tests/optional_test.cc
Original file line number Diff line number Diff line change
Expand Up @@ -108,3 +108,14 @@ TEST(OptionalTest, CopyConstructorNullTest)
const optional_t<int> v1(v2);
EXPECT_FALSE(v1.has_value());
}

TEST(OptionalTest, SelfAssignmentTest)
{
optional_t<int> 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<int>& alias = v;
v = alias;
EXPECT_TRUE(v.has_value());
EXPECT_EQ(5, v.value());
}
Loading