Merge pull request #2 from KABoissonneault/trivial_copy_fix

Fixed copy and move operations on "expected<trivially_copyable,nontri…
This commit is contained in:
Simon Brand
2017-12-05 20:20:29 +00:00
committed by GitHub
2 changed files with 71 additions and 1 deletions

View File

@@ -612,7 +612,15 @@ struct expected_copy_base<T, E, false> : expected_operations_base<T, E> {
} }
expected_copy_base(expected_copy_base &&rhs) = default; expected_copy_base(expected_copy_base &&rhs) = default;
expected_copy_base &operator=(const expected_copy_base &rhs) = default; expected_copy_base &operator=(const expected_copy_base &rhs) {
if (rhs.has_value()) {
this->construct(rhs.get());
}
else {
this->construct_error(rhs.geterr());
}
return *this;
}
expected_copy_base &operator=(expected_copy_base &&rhs) = default; expected_copy_base &operator=(expected_copy_base &&rhs) = default;
}; };
@@ -713,6 +721,7 @@ struct expected_move_assign_base<T, E, false>
operator=(const expected_move_assign_base &rhs) = default; operator=(const expected_move_assign_base &rhs) = default;
expected_move_assign_base & expected_move_assign_base &
operator=(expected_move_assign_base &&rhs) noexcept( operator=(expected_move_assign_base &&rhs) noexcept(
std::is_nothrow_move_constructible<T>::value std::is_nothrow_move_constructible<T>::value
&&std::is_nothrow_move_assignable<T>::value) { &&std::is_nothrow_move_assignable<T>::value) {

View File

@@ -2,6 +2,8 @@
#include "expected.hpp" #include "expected.hpp"
#include <vector> #include <vector>
#include <type_traits>
#include <string>
struct takes_init_and_variadic { struct takes_init_and_variadic {
std::vector<int> v; std::vector<int> v;
@@ -59,4 +61,63 @@ TEST_CASE("Constructors", "[constructors]") {
REQUIRE(std::get<1>(e->t) == 3); REQUIRE(std::get<1>(e->t) == 3);
} }
{
tl::expected<int, int> e;
REQUIRE(std::is_default_constructible<decltype(e)>::value);
REQUIRE(std::is_copy_constructible<decltype(e)>::value);
REQUIRE(std::is_move_constructible<decltype(e)>::value);
REQUIRE(std::is_copy_assignable<decltype(e)>::value);
REQUIRE(std::is_move_assignable<decltype(e)>::value);
REQUIRE(IS_TRIVIALLY_COPY_CONSTRUCTIBLE(decltype(e)));
REQUIRE(IS_TRIVIALLY_COPY_ASSIGNABLE(decltype(e)));
# if !defined(TL_EXPECTED_GCC49)
REQUIRE(std::is_trivially_move_constructible<decltype(e)>::value);
REQUIRE(std::is_trivially_move_assignable<decltype(e)>::value);
# endif
}
{
tl::expected<int, std::string> e;
REQUIRE(std::is_default_constructible<decltype(e)>::value);
REQUIRE(std::is_copy_constructible<decltype(e)>::value);
REQUIRE(std::is_move_constructible<decltype(e)>::value);
REQUIRE(std::is_copy_assignable<decltype(e)>::value);
REQUIRE(std::is_move_assignable<decltype(e)>::value);
REQUIRE(!IS_TRIVIALLY_COPY_CONSTRUCTIBLE(decltype(e)));
REQUIRE(!IS_TRIVIALLY_COPY_ASSIGNABLE(decltype(e)));
# if !defined(TL_EXPECTED_GCC49)
REQUIRE(!std::is_trivially_move_constructible<decltype(e)>::value);
REQUIRE(!std::is_trivially_move_assignable<decltype(e)>::value);
# endif
}
{
tl::expected<std::string, int> e;
REQUIRE(std::is_default_constructible<decltype(e)>::value);
REQUIRE(std::is_copy_constructible<decltype(e)>::value);
REQUIRE(std::is_move_constructible<decltype(e)>::value);
REQUIRE(std::is_copy_assignable<decltype(e)>::value);
REQUIRE(std::is_move_assignable<decltype(e)>::value);
REQUIRE(!IS_TRIVIALLY_COPY_CONSTRUCTIBLE(decltype(e)));
REQUIRE(!IS_TRIVIALLY_COPY_ASSIGNABLE(decltype(e)));
# if !defined(TL_EXPECTED_GCC49)
REQUIRE(!std::is_trivially_move_constructible<decltype(e)>::value);
REQUIRE(!std::is_trivially_move_assignable<decltype(e)>::value);
# endif
}
{
tl::expected<std::string, std::string> e;
REQUIRE(std::is_default_constructible<decltype(e)>::value);
REQUIRE(std::is_copy_constructible<decltype(e)>::value);
REQUIRE(std::is_move_constructible<decltype(e)>::value);
REQUIRE(std::is_copy_assignable<decltype(e)>::value);
REQUIRE(std::is_move_assignable<decltype(e)>::value);
REQUIRE(!IS_TRIVIALLY_COPY_CONSTRUCTIBLE(decltype(e)));
REQUIRE(!IS_TRIVIALLY_COPY_ASSIGNABLE(decltype(e)));
# if !defined(TL_EXPECTED_GCC49)
REQUIRE(!std::is_trivially_move_constructible<decltype(e)>::value);
REQUIRE(!std::is_trivially_move_assignable<decltype(e)>::value);
# endif
}
} }