add swap noexcept

This commit is contained in:
2019-05-12 17:12:45 +07:00
parent 574de9c995
commit 05934cac38
8 changed files with 118 additions and 142 deletions

View File

@@ -319,7 +319,10 @@ namespace flat_hpp
: 0; : 0;
} }
void swap(flat_map& other) { void swap(flat_map& other)
noexcept(std::is_nothrow_swappable_v<base_type>
&& std::is_nothrow_swappable_v<container_type>)
{
using std::swap; using std::swap;
swap( swap(
static_cast<base_type&>(*this), static_cast<base_type&>(*this),
@@ -397,6 +400,7 @@ namespace flat_hpp
void swap( void swap(
flat_map<Key, Value, Compare, Container>& l, flat_map<Key, Value, Compare, Container>& l,
flat_map<Key, Value, Compare, Container>& r) flat_map<Key, Value, Compare, Container>& r)
noexcept(noexcept(l.swap(r)))
{ {
l.swap(r); l.swap(r);
} }

View File

@@ -315,7 +315,10 @@ namespace flat_hpp
return r; return r;
} }
void swap(flat_multimap& other) { void swap(flat_multimap& other)
noexcept(std::is_nothrow_swappable_v<base_type>
&& std::is_nothrow_swappable_v<container_type>)
{
using std::swap; using std::swap;
swap( swap(
static_cast<base_type&>(*this), static_cast<base_type&>(*this),
@@ -393,6 +396,7 @@ namespace flat_hpp
void swap( void swap(
flat_multimap<Key, Value, Compare, Container>& l, flat_multimap<Key, Value, Compare, Container>& l,
flat_multimap<Key, Value, Compare, Container>& r) flat_multimap<Key, Value, Compare, Container>& r)
noexcept(noexcept(l.swap(r)))
{ {
l.swap(r); l.swap(r);
} }

View File

@@ -230,7 +230,10 @@ namespace flat_hpp
return r; return r;
} }
void swap(flat_multiset& other) { void swap(flat_multiset& other)
noexcept(std::is_nothrow_swappable_v<base_type>
&& std::is_nothrow_swappable_v<container_type>)
{
using std::swap; using std::swap;
swap( swap(
static_cast<base_type&>(*this), static_cast<base_type&>(*this),
@@ -301,6 +304,7 @@ namespace flat_hpp
void swap( void swap(
flat_multiset<Key, Compare, Container>& l, flat_multiset<Key, Compare, Container>& l,
flat_multiset<Key, Compare, Container>& r) flat_multiset<Key, Compare, Container>& r)
noexcept(noexcept(l.swap(r)))
{ {
l.swap(r); l.swap(r);
} }

View File

@@ -234,7 +234,10 @@ namespace flat_hpp
: 0; : 0;
} }
void swap(flat_set& other) { void swap(flat_set& other)
noexcept(std::is_nothrow_swappable_v<base_type>
&& std::is_nothrow_swappable_v<container_type>)
{
using std::swap; using std::swap;
swap( swap(
static_cast<base_type&>(*this), static_cast<base_type&>(*this),
@@ -305,6 +308,7 @@ namespace flat_hpp
void swap( void swap(
flat_set<Key, Compare, Container>& l, flat_set<Key, Compare, Container>& l,
flat_set<Key, Compare, Container>& r) flat_set<Key, Compare, Container>& r)
noexcept(noexcept(l.swap(r)))
{ {
l.swap(r); l.swap(r);
} }

View File

@@ -38,6 +38,15 @@ namespace
void swap(dummy_less2<T>&, dummy_less2<T>&) noexcept { void swap(dummy_less2<T>&, dummy_less2<T>&) noexcept {
} }
template < typename T >
class dummy_less3 {
dummy_less3() = default;
dummy_less3(dummy_less3&&) noexcept(false) {}
bool operator()(const T& l, const T& r) const {
return l < r;
}
};
template < typename T > template < typename T >
constexpr std::add_const_t<T>& my_as_const(T& t) noexcept { constexpr std::add_const_t<T>& my_as_const(T& t) noexcept {
return t; return t;
@@ -57,50 +66,30 @@ TEST_CASE("flat_map") {
using alloc_t = std::allocator<std::pair<int,unsigned>>; using alloc_t = std::allocator<std::pair<int,unsigned>>;
using map_t = flat_map<int, unsigned, dummy_less<int>, std::vector<std::pair<int,unsigned>, alloc_t>>; using map_t = flat_map<int, unsigned, dummy_less<int>, std::vector<std::pair<int,unsigned>, alloc_t>>;
using map2_t = flat_map<int, unsigned, dummy_less2<int>>; using map2_t = flat_map<int, unsigned, dummy_less2<int>>;
using map3_t = flat_map<int, unsigned, dummy_less3<int>>;
static_assert( STATIC_REQUIRE(std::is_nothrow_default_constructible_v<map_t>);
std::is_nothrow_default_constructible_v<map_t>, STATIC_REQUIRE(std::is_nothrow_move_constructible_v<map_t>);
"unit test static error"); STATIC_REQUIRE(std::is_nothrow_move_assignable_v<map_t>);
static_assert( STATIC_REQUIRE(std::is_nothrow_swappable_v<map_t>);
std::is_nothrow_move_constructible_v<map_t>, STATIC_REQUIRE(std::is_nothrow_swappable_v<map2_t>);
"unit test static error"); STATIC_REQUIRE(!std::is_nothrow_swappable_v<map3_t>);
static_assert(
std::is_nothrow_move_assignable_v<map_t>,
"unit test static error");
} }
SECTION("types") { SECTION("types") {
using map_t = flat_map<int, unsigned>; using map_t = flat_map<int, unsigned>;
static_assert( STATIC_REQUIRE(std::is_same_v<map_t::key_type, int>);
std::is_same_v<map_t::key_type, int>, STATIC_REQUIRE(std::is_same_v<map_t::mapped_type, unsigned>);
"unit test static error"); STATIC_REQUIRE(std::is_same_v<map_t::value_type, std::pair<int, unsigned>>);
static_assert(
std::is_same_v<map_t::mapped_type, unsigned>,
"unit test static error");
static_assert(
std::is_same_v<map_t::value_type, std::pair<int, unsigned>>,
"unit test static error");
static_assert( STATIC_REQUIRE(std::is_same_v<map_t::size_type, std::size_t>);
std::is_same_v<map_t::size_type, std::size_t>, STATIC_REQUIRE(std::is_same_v<map_t::difference_type, std::ptrdiff_t>);
"unit test static error");
static_assert(
std::is_same_v<map_t::difference_type, std::ptrdiff_t>,
"unit test static error");
static_assert( STATIC_REQUIRE(std::is_same_v<map_t::reference, std::pair<int, unsigned>&>);
std::is_same_v<map_t::reference, std::pair<int, unsigned>&>, STATIC_REQUIRE(std::is_same_v<map_t::const_reference, const std::pair<int, unsigned>&>);
"unit test static error");
static_assert(
std::is_same_v<map_t::const_reference, const std::pair<int, unsigned>&>,
"unit test static error");
static_assert( STATIC_REQUIRE(std::is_same_v<map_t::pointer, std::pair<int, unsigned>*>);
std::is_same_v<map_t::pointer, std::pair<int, unsigned>*>, STATIC_REQUIRE(std::is_same_v<map_t::const_pointer, const std::pair<int, unsigned>*>);
"unit test static error");
static_assert(
std::is_same_v<map_t::const_pointer, const std::pair<int, unsigned>*>,
"unit test static error");
} }
SECTION("ctors") { SECTION("ctors") {
using alloc_t = std::allocator< using alloc_t = std::allocator<

View File

@@ -38,6 +38,15 @@ namespace
void swap(dummy_less2<T>&, dummy_less2<T>&) noexcept { void swap(dummy_less2<T>&, dummy_less2<T>&) noexcept {
} }
template < typename T >
class dummy_less3 {
dummy_less3() = default;
dummy_less3(dummy_less3&&) noexcept(false) {}
bool operator()(const T& l, const T& r) const {
return l < r;
}
};
template < typename T > template < typename T >
constexpr std::add_const_t<T>& my_as_const(T& t) noexcept { constexpr std::add_const_t<T>& my_as_const(T& t) noexcept {
return t; return t;
@@ -57,50 +66,30 @@ TEST_CASE("flat_multimap") {
using alloc_t = std::allocator<std::pair<int,unsigned>>; using alloc_t = std::allocator<std::pair<int,unsigned>>;
using map_t = flat_multimap<int, unsigned, dummy_less<int>, std::vector<std::pair<int,unsigned>, alloc_t>>; using map_t = flat_multimap<int, unsigned, dummy_less<int>, std::vector<std::pair<int,unsigned>, alloc_t>>;
using map2_t = flat_multimap<int, unsigned, dummy_less2<int>>; using map2_t = flat_multimap<int, unsigned, dummy_less2<int>>;
using map3_t = flat_multimap<int, unsigned, dummy_less3<int>>;
static_assert( STATIC_REQUIRE(std::is_nothrow_default_constructible_v<map_t>);
std::is_nothrow_default_constructible_v<map_t>, STATIC_REQUIRE(std::is_nothrow_move_constructible_v<map_t>);
"unit test static error"); STATIC_REQUIRE(std::is_nothrow_move_assignable_v<map_t>);
static_assert( STATIC_REQUIRE(std::is_nothrow_swappable_v<map_t>);
std::is_nothrow_move_constructible_v<map_t>, STATIC_REQUIRE(std::is_nothrow_swappable_v<map2_t>);
"unit test static error"); STATIC_REQUIRE(!std::is_nothrow_swappable_v<map3_t>);
static_assert(
std::is_nothrow_move_assignable_v<map_t>,
"unit test static error");
} }
SECTION("types") { SECTION("types") {
using map_t = flat_multimap<int, unsigned>; using map_t = flat_multimap<int, unsigned>;
static_assert( STATIC_REQUIRE(std::is_same_v<map_t::key_type, int>);
std::is_same_v<map_t::key_type, int>, STATIC_REQUIRE(std::is_same_v<map_t::mapped_type, unsigned>);
"unit test static error"); STATIC_REQUIRE(std::is_same_v<map_t::value_type, std::pair<int, unsigned>>);
static_assert(
std::is_same_v<map_t::mapped_type, unsigned>,
"unit test static error");
static_assert(
std::is_same_v<map_t::value_type, std::pair<int, unsigned>>,
"unit test static error");
static_assert( STATIC_REQUIRE(std::is_same_v<map_t::size_type, std::size_t>);
std::is_same_v<map_t::size_type, std::size_t>, STATIC_REQUIRE(std::is_same_v<map_t::difference_type, std::ptrdiff_t>);
"unit test static error");
static_assert(
std::is_same_v<map_t::difference_type, std::ptrdiff_t>,
"unit test static error");
static_assert( STATIC_REQUIRE(std::is_same_v<map_t::reference, std::pair<int, unsigned>&>);
std::is_same_v<map_t::reference, std::pair<int, unsigned>&>, STATIC_REQUIRE(std::is_same_v<map_t::const_reference, const std::pair<int, unsigned>&>);
"unit test static error");
static_assert(
std::is_same_v<map_t::const_reference, const std::pair<int, unsigned>&>,
"unit test static error");
static_assert( STATIC_REQUIRE(std::is_same_v<map_t::pointer, std::pair<int, unsigned>*>);
std::is_same_v<map_t::pointer, std::pair<int, unsigned>*>, STATIC_REQUIRE(std::is_same_v<map_t::const_pointer, const std::pair<int, unsigned>*>);
"unit test static error");
static_assert(
std::is_same_v<map_t::const_pointer, const std::pair<int, unsigned>*>,
"unit test static error");
} }
SECTION("ctors") { SECTION("ctors") {
using alloc_t = std::allocator< using alloc_t = std::allocator<

View File

@@ -38,6 +38,15 @@ namespace
void swap(dummy_less2<T>&, dummy_less2<T>&) noexcept { void swap(dummy_less2<T>&, dummy_less2<T>&) noexcept {
} }
template < typename T >
class dummy_less3 {
dummy_less3() = default;
dummy_less3(dummy_less3&&) noexcept(false) {}
bool operator()(const T& l, const T& r) const {
return l < r;
}
};
template < typename T > template < typename T >
constexpr std::add_const_t<T>& my_as_const(T& t) noexcept { constexpr std::add_const_t<T>& my_as_const(T& t) noexcept {
return t; return t;
@@ -57,47 +66,29 @@ TEST_CASE("flat_multiset") {
using alloc_t = std::allocator<int>; using alloc_t = std::allocator<int>;
using set_t = flat_multiset<int, dummy_less<int>, std::vector<int, alloc_t>>; using set_t = flat_multiset<int, dummy_less<int>, std::vector<int, alloc_t>>;
using set2_t = flat_multiset<int, dummy_less2<int>>; using set2_t = flat_multiset<int, dummy_less2<int>>;
using set3_t = flat_multiset<int, dummy_less3<int>>;
static_assert( STATIC_REQUIRE(std::is_nothrow_default_constructible_v<set_t>);
std::is_nothrow_default_constructible_v<set_t>, STATIC_REQUIRE(std::is_nothrow_move_constructible_v<set_t>);
"unit test static error"); STATIC_REQUIRE(std::is_nothrow_move_assignable_v<set_t>);
static_assert( STATIC_REQUIRE(std::is_nothrow_swappable_v<set_t>);
std::is_nothrow_move_constructible_v<set_t>, STATIC_REQUIRE(std::is_nothrow_swappable_v<set2_t>);
"unit test static error"); STATIC_REQUIRE(!std::is_nothrow_swappable_v<set3_t>);
static_assert(
std::is_nothrow_move_assignable_v<set_t>,
"unit test static error");
} }
SECTION("types") { SECTION("types") {
using set_t = flat_multiset<int>; using set_t = flat_multiset<int>;
static_assert( STATIC_REQUIRE(std::is_same_v<set_t::key_type, int>);
std::is_same_v<set_t::key_type, int>, STATIC_REQUIRE(std::is_same_v<set_t::value_type, int>);
"unit test static error");
static_assert(
std::is_same_v<set_t::value_type, int>,
"unit test static error");
static_assert( STATIC_REQUIRE(std::is_same_v<set_t::size_type, std::size_t>);
std::is_same_v<set_t::size_type, std::size_t>, STATIC_REQUIRE(std::is_same_v<set_t::difference_type, std::ptrdiff_t>);
"unit test static error");
static_assert(
std::is_same_v<set_t::difference_type, std::ptrdiff_t>,
"unit test static error");
static_assert( STATIC_REQUIRE(std::is_same_v<set_t::reference, int&>);
std::is_same_v<set_t::reference, int&>, STATIC_REQUIRE(std::is_same_v<set_t::const_reference, const int&>);
"unit test static error");
static_assert(
std::is_same_v<set_t::const_reference, const int&>,
"unit test static error");
static_assert( STATIC_REQUIRE(std::is_same_v<set_t::pointer, int*>);
std::is_same_v<set_t::pointer, int*>, STATIC_REQUIRE(std::is_same_v<set_t::const_pointer, const int*>);
"unit test static error");
static_assert(
std::is_same_v<set_t::const_pointer, const int*>,
"unit test static error");
} }
SECTION("ctors") { SECTION("ctors") {
using alloc_t = std::allocator<int>; using alloc_t = std::allocator<int>;

View File

@@ -38,6 +38,15 @@ namespace
void swap(dummy_less2<T>&, dummy_less2<T>&) noexcept { void swap(dummy_less2<T>&, dummy_less2<T>&) noexcept {
} }
template < typename T >
class dummy_less3 {
dummy_less3() = default;
dummy_less3(dummy_less3&&) noexcept(false) {}
bool operator()(const T& l, const T& r) const {
return l < r;
}
};
template < typename T > template < typename T >
constexpr std::add_const_t<T>& my_as_const(T& t) noexcept { constexpr std::add_const_t<T>& my_as_const(T& t) noexcept {
return t; return t;
@@ -57,47 +66,29 @@ TEST_CASE("flat_set") {
using alloc_t = std::allocator<int>; using alloc_t = std::allocator<int>;
using set_t = flat_set<int, dummy_less<int>, std::vector<int, alloc_t>>; using set_t = flat_set<int, dummy_less<int>, std::vector<int, alloc_t>>;
using set2_t = flat_set<int, dummy_less2<int>>; using set2_t = flat_set<int, dummy_less2<int>>;
using set3_t = flat_set<int, dummy_less3<int>>;
static_assert( STATIC_REQUIRE(std::is_nothrow_default_constructible_v<set_t>);
std::is_nothrow_default_constructible_v<set_t>, STATIC_REQUIRE(std::is_nothrow_move_constructible_v<set_t>);
"unit test static error"); STATIC_REQUIRE(std::is_nothrow_move_assignable_v<set_t>);
static_assert( STATIC_REQUIRE(std::is_nothrow_swappable_v<set_t>);
std::is_nothrow_move_constructible_v<set_t>, STATIC_REQUIRE(std::is_nothrow_swappable_v<set2_t>);
"unit test static error"); STATIC_REQUIRE(!std::is_nothrow_swappable_v<set3_t>);
static_assert(
std::is_nothrow_move_assignable_v<set_t>,
"unit test static error");
} }
SECTION("types") { SECTION("types") {
using set_t = flat_set<int>; using set_t = flat_set<int>;
static_assert( STATIC_REQUIRE(std::is_same_v<set_t::key_type, int>);
std::is_same_v<set_t::key_type, int>, STATIC_REQUIRE(std::is_same_v<set_t::value_type, int>);
"unit test static error");
static_assert(
std::is_same_v<set_t::value_type, int>,
"unit test static error");
static_assert( STATIC_REQUIRE(std::is_same_v<set_t::size_type, std::size_t>);
std::is_same_v<set_t::size_type, std::size_t>, STATIC_REQUIRE(std::is_same_v<set_t::difference_type, std::ptrdiff_t>);
"unit test static error");
static_assert(
std::is_same_v<set_t::difference_type, std::ptrdiff_t>,
"unit test static error");
static_assert( STATIC_REQUIRE(std::is_same_v<set_t::reference, int&>);
std::is_same_v<set_t::reference, int&>, STATIC_REQUIRE(std::is_same_v<set_t::const_reference, const int&>);
"unit test static error");
static_assert(
std::is_same_v<set_t::const_reference, const int&>,
"unit test static error");
static_assert( STATIC_REQUIRE(std::is_same_v<set_t::pointer, int*>);
std::is_same_v<set_t::pointer, int*>, STATIC_REQUIRE(std::is_same_v<set_t::const_pointer, const int*>);
"unit test static error");
static_assert(
std::is_same_v<set_t::const_pointer, const int*>,
"unit test static error");
} }
SECTION("ctors") { SECTION("ctors") {
using alloc_t = std::allocator<int>; using alloc_t = std::allocator<int>;