Skip to content

Commit

Permalink
Merge pull request #33 from mapbox/fix_assignment_operator
Browse files Browse the repository at this point in the history
fix assignment of optionals, adds a unit test
  • Loading branch information
artemp committed Oct 22, 2014
2 parents eb99515 + f69f317 commit c56af22
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 9 deletions.
9 changes: 0 additions & 9 deletions optional.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -33,15 +33,6 @@ template <typename T> class optional

optional(T const &v) { variant_ = v; }

optional &operator=(optional other)
{ // note: argument passed by value!
if (this != &other)
{
swap(other);
}
return *this;
}

explicit operator bool() const noexcept { return variant_.template is<T>(); }

T const &get() const { return variant_.template get<T>(); }
Expand Down
11 changes: 11 additions & 0 deletions test/optional_unit.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -67,5 +67,16 @@ TEST_CASE( "emplace initialization, reset", "[optional]" ) {

dummy_opt.reset();
REQUIRE(!dummy_opt);
}

TEST_CASE( "assignment", "[optional]") {
mapbox::util::optional<int> a;
mapbox::util::optional<int> b;

a = 1; b = 3;
REQUIRE(a.get() == 1);
REQUIRE(b.get() == 3);
b = a;
REQUIRE(a.get() == b.get());
REQUIRE(b.get() == 1);
}

0 comments on commit c56af22

Please sign in to comment.