Skip to content

Commit

Permalink
Fixing optional::swap
Browse files Browse the repository at this point in the history
  • Loading branch information
hkaiser committed Feb 26, 2018
1 parent a3faea3 commit 75d72b8
Show file tree
Hide file tree
Showing 3 changed files with 33 additions and 3 deletions.
18 changes: 15 additions & 3 deletions hpx/util/optional.hpp
Expand Up @@ -42,6 +42,15 @@ namespace hpx { namespace util
}
};

namespace _optional_swap
{
using std::swap;

template <typename T>
void check_swap(T&, T&) noexcept(noexcept(
swap(std::declval<T&>(), std::declval<T&>())));
}

template <typename T>
class optional
{
Expand Down Expand Up @@ -249,9 +258,11 @@ namespace hpx { namespace util
empty_ = false;
}

void swap(optional& other)
noexcept(std::is_nothrow_move_constructible<T>::value &&
noexcept(swap(std::declval<T&>(), std::declval<T&>())))
void swap(optional& other) noexcept(
std::is_nothrow_move_constructible<T>::value &&
noexcept(
_optional_swap::check_swap(std::declval<T&>(),
std::declval<T&>())))
{
// do nothing if both are empty
if (empty_ && other.empty_)
Expand All @@ -261,6 +272,7 @@ namespace hpx { namespace util
// swap content if both are non-empty
if (!empty_ && !other.empty_)
{
using std::swap;
swap(**this, *other);
return;
}
Expand Down
1 change: 1 addition & 0 deletions tests/regressions/util/CMakeLists.txt
Expand Up @@ -11,6 +11,7 @@ set(tests
function_argument
function_serialization_728
iarchive_1237
optional_swap_3200
protect_with_nullary_pfo
serialize_buffer_1069
set_config_entry_deadlock
Expand Down
17 changes: 17 additions & 0 deletions tests/regressions/util/optional_swap_3200.cpp
@@ -0,0 +1,17 @@
// Copyright (c) 2018 Hartmut Kaiser
//
// Distributed under the Boost Software License, Version 1.0. (See accompanying
// file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)

#include <hpx/hpx_main.hpp>
#include <hpx/util/optional.hpp>
#include <hpx/util/lightweight_test.hpp>

int main()
{
hpx::util::optional<int> x;
int y = 1;
x = y;

return hpx::util::report_errors();
}

0 comments on commit 75d72b8

Please sign in to comment.