Skip to content

Commit aef62df

Browse files
committed
Merge branch 'master' into cmake_love
2 parents 359499d + d613d34 commit aef62df

File tree

3 files changed

+30
-2
lines changed

3 files changed

+30
-2
lines changed

CMakeLists.txt

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,8 @@ if(OPTIONAL_ENABLE_TESTS)
3939
${CMAKE_CURRENT_SOURCE_DIR}/tests/assignment.cpp
4040
${CMAKE_CURRENT_SOURCE_DIR}/tests/issues.cpp
4141
${CMAKE_CURRENT_SOURCE_DIR}/tests/bases.cpp
42-
${CMAKE_CURRENT_SOURCE_DIR}/tests/nullopt.cpp)
42+
${CMAKE_CURRENT_SOURCE_DIR}/tests/nullopt.cpp
43+
${CMAKE_CURRENT_SOURCE_DIR}/tests/swap.cpp)
4344

4445
add_executable(tests ${TEST_SOURCES})
4546

include/tl/optional.hpp

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1239,9 +1239,9 @@ class optional : private detail::optional_move_assign_base<T>,
12391239
void
12401240
swap(optional &rhs) noexcept(std::is_nothrow_move_constructible<T>::value
12411241
&&detail::is_nothrow_swappable<T>::value) {
1242+
using std::swap;
12421243
if (has_value()) {
12431244
if (rhs.has_value()) {
1244-
using std::swap;
12451245
swap(**this, *rhs);
12461246
} else {
12471247
new (std::addressof(rhs.m_value)) T(std::move(this->m_value));
@@ -1251,6 +1251,7 @@ class optional : private detail::optional_move_assign_base<T>,
12511251
new (std::addressof(this->m_value)) T(std::move(rhs.m_value));
12521252
rhs.m_value.T::~T();
12531253
}
1254+
swap(this->m_has_value, rhs.m_has_value);
12541255
}
12551256

12561257
/// Returns a pointer to the stored value

tests/swap.cpp

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
#include "catch.hpp"
2+
#include "optional.hpp"
3+
4+
TEST_CASE("Swap value", "[swap.value]") {
5+
tl::optional<int> o1 = 42;
6+
tl::optional<int> o2 = 12;
7+
o1.swap(o2);
8+
CHECK(o1.value() == 12);
9+
CHECK(o2.value() == 42);
10+
}
11+
12+
TEST_CASE("Swap value with null intialized", "[swap.value_nullopt]") {
13+
tl::optional<int> o1 = 42;
14+
tl::optional<int> o2 = tl::nullopt;
15+
o1.swap(o2);
16+
CHECK(!o1.has_value());
17+
CHECK(o2.value() == 42);
18+
}
19+
20+
TEST_CASE("Swap null intialized with value", "[swap.nullopt_value]") {
21+
tl::optional<int> o1 = tl::nullopt;
22+
tl::optional<int> o2 = 42;
23+
o1.swap(o2);
24+
CHECK(o1.value() == 42);
25+
CHECK(!o2.has_value());
26+
}

0 commit comments

Comments
 (0)