Skip to content

Commit 5e306bf

Browse files
committed
address comment
1 parent 59b682d commit 5e306bf

File tree

21 files changed

+136
-340
lines changed

21 files changed

+136
-340
lines changed

libcxx/include/__flat_map/flat_map.h

Lines changed: 6 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -789,20 +789,12 @@ class flat_map {
789789
}
790790

791791
_LIBCPP_HIDE_FROM_ABI void swap(flat_map& __y) noexcept {
792-
# ifndef _LIBCPP_HAS_NO_EXCEPTIONS
793-
try {
794-
# endif
795-
ranges::swap(__compare_, __y.__compare_);
796-
ranges::swap(__containers_.keys, __y.__containers_.keys);
797-
ranges::swap(__containers_.values, __y.__containers_.values);
798-
# ifndef _LIBCPP_HAS_NO_EXCEPTIONS
799-
} catch (...) {
800-
// todo: how can we tell the user that the swap is unsuccessful?
801-
// need to swallow the exception because the function is noexcept
802-
clear() /* noexcept */;
803-
__y.clear() /*noexcept*/;
804-
}
805-
# endif
792+
// warning: The spec has unconditional noexcept, which means that
793+
// if any of the following functions throw an exception,
794+
// std::terminate will be called
795+
ranges::swap(__compare_, __y.__compare_);
796+
ranges::swap(__containers_.keys, __y.__containers_.keys);
797+
ranges::swap(__containers_.values, __y.__containers_.values);
806798
}
807799

808800
_LIBCPP_HIDE_FROM_ABI void clear() noexcept {

libcxx/include/flat_map

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
77
//
88
//===----------------------------------------------------------------------===//
9+
910
#ifndef _LIBCPP_FLAT_MAP
1011
#define _LIBCPP_FLAT_MAP
1112

libcxx/test/libcxx/containers/containers.adaptors/flat.map/assert.input_range.pass.cpp

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
55
//
66
//===----------------------------------------------------------------------===//
7+
78
// REQUIRES: has-unix-headers
89
// UNSUPPORTED: c++03, c++11, c++14, c++17, c++20
910
// UNSUPPORTED: libcpp-hardening-mode=none
@@ -18,7 +19,9 @@
1819
//
1920

2021
#include <flat_map>
21-
#include <cassert>
22+
#include <functional>
23+
#include <memory>
24+
#include <vector>
2225

2326
#include "check_assertion.h"
2427

libcxx/test/libcxx/containers/containers.adaptors/flat.map/assert.sorted_unique.pass.cpp

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
55
//
66
//===----------------------------------------------------------------------===//
7+
78
// REQUIRES: has-unix-headers
89
// UNSUPPORTED: c++03, c++11, c++14, c++17, c++20
910
// UNSUPPORTED: libcpp-hardening-mode=none
@@ -18,7 +19,11 @@
1819
//
1920

2021
#include <flat_map>
21-
#include <cassert>
22+
#include <functional>
23+
#include <initializer_list>
24+
#include <memory>
25+
#include <utility>
26+
#include <vector>
2227

2328
#include "check_assertion.h"
2429

libcxx/test/std/containers/container.adaptors/NaiveStaticVector.h

Lines changed: 18 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -9,31 +9,37 @@
99
#ifndef SUPPORT_NAIVE_STATIC_VECTOR_H
1010
#define SUPPORT_NAIVE_STATIC_VECTOR_H
1111

12+
#include <cstddef>
13+
#include <utility>
1214
#include "test_iterators.h"
1315
#include "test_macros.h"
1416

15-
template<class T, size_t N>
17+
template <class T, std::size_t N>
1618
struct NaiveStaticVector {
1719
struct CapacityError {};
1820

19-
using value_type = T;
21+
using value_type = T;
2022
using difference_type = short;
21-
using size_type = unsigned short;
22-
using iterator = random_access_iterator<T*>;
23-
using const_iterator = random_access_iterator<const T*>;
23+
using size_type = unsigned short;
24+
using iterator = random_access_iterator<T*>;
25+
using const_iterator = random_access_iterator<const T*>;
2426

2527
explicit NaiveStaticVector() = default;
26-
template<class It> explicit NaiveStaticVector(It first, It last) { while (first != last) insert(*first++); }
28+
template <class It>
29+
explicit NaiveStaticVector(It first, It last) {
30+
while (first != last)
31+
insert(*first++);
32+
}
2733

2834
// Moving-from a NaiveStaticVector leaves the source vector holding moved-from objects.
2935
// This is intentional (the "Naive" in the name).
3036
// Specifically, moving-out-of a sorted+uniqued NaiveStaticVector<MoveOnly>
3137
// will leave it in a non-sorted+uniqued state.
3238

33-
NaiveStaticVector(const NaiveStaticVector&) = default;
34-
NaiveStaticVector(NaiveStaticVector&&) = default; // deliberately don't reset size_
39+
NaiveStaticVector(const NaiveStaticVector&) = default;
40+
NaiveStaticVector(NaiveStaticVector&&) = default; // deliberately don't reset size_
3541
NaiveStaticVector& operator=(const NaiveStaticVector&) = default;
36-
NaiveStaticVector& operator=(NaiveStaticVector&&) = default;
42+
NaiveStaticVector& operator=(NaiveStaticVector&&) = default;
3743

3844
iterator begin() { return iterator(data_); }
3945
const_iterator begin() const { return const_iterator(data_); }
@@ -45,7 +51,7 @@ struct NaiveStaticVector {
4551

4652
void clear() { size_ = 0; }
4753

48-
template<class It>
54+
template <class It>
4955
iterator insert(const_iterator pos, It first, It last) {
5056
iterator result = pos - cbegin() + begin();
5157
while (first != last) {
@@ -78,13 +84,11 @@ struct NaiveStaticVector {
7884
return begin() + i;
7985
}
8086

81-
iterator erase(const_iterator pos) {
82-
return erase(pos, std::next(pos));
83-
}
87+
iterator erase(const_iterator pos) { return erase(pos, std::next(pos)); }
8488

8589
private:
8690
T data_[N];
87-
size_t size_ = 0;
91+
std::size_t size_ = 0;
8892
};
8993

9094
#endif // SUPPORT_NAIVE_STATIC_VECTOR_H
Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
//===----------------------------------------------------------------------===//
2+
//
3+
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4+
// See https://llvm.org/LICENSE.txt for license information.
5+
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6+
//
7+
//===----------------------------------------------------------------------===//
8+
9+
// UNSUPPORTED: c++03, c++11, c++14, c++17, c++20
10+
11+
// <flat_map>
12+
13+
// struct sorted_unique_t { explicit sorted_unique_t() = default; };
14+
// inline constexpr sorted_unique_t sorted_unique{};
15+
16+
#include <cassert>
17+
#include <concepts>
18+
#include <flat_map>
19+
#include <type_traits>
20+
21+
template <class T>
22+
void implicit_test(T) {}
23+
24+
template <class T>
25+
concept HasImplicitDefaultCtor = requires { implicit_test<T>({}); };
26+
27+
static_assert(std::is_default_constructible_v<std::sorted_unique_t>);
28+
static_assert(std::is_trivially_default_constructible_v<std::sorted_unique_t>);
29+
static_assert(!HasImplicitDefaultCtor<std::sorted_unique_t>);
30+
31+
constexpr bool test() {
32+
{
33+
[[maybe_unused]] std::sorted_unique_t s;
34+
}
35+
{
36+
[[maybe_unused]] std::same_as<const std::sorted_unique_t&> decltype(auto) s = (std::sorted_unique);
37+
}
38+
{
39+
[[maybe_unused]] std::same_as<const std::sorted_unique_t> decltype(auto) copy = std::sorted_unique;
40+
}
41+
42+
return true;
43+
}
44+
45+
int main(int, char**) {
46+
test();
47+
static_assert(test());
48+
}

libcxx/test/std/containers/container.adaptors/flat.map/flat.map.capacity/empty.pass.cpp

Lines changed: 1 addition & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -46,20 +46,6 @@ int main(int, char**) {
4646
m.clear();
4747
assert(m.empty());
4848
}
49-
#if 0
50-
// vector<bool> is not supported
51-
{
52-
typedef std::flat_map<bool, bool> M;
53-
M m;
54-
ASSERT_SAME_TYPE(decltype(m.empty()), bool);
55-
ASSERT_NOEXCEPT(m.empty());
56-
assert(m.empty());
57-
assert(std::as_const(m).empty());
58-
m = {{false, false}};
59-
assert(!m.empty());
60-
m.clear();
61-
assert(m.empty());
62-
}
63-
#endif
49+
6450
return 0;
6551
}

libcxx/test/std/containers/container.adaptors/flat.map/flat.map.capacity/max_size.pass.cpp

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@ int main(int, char**) {
2929
using C = std::flat_map<int, int, std::less<int>, std::vector<int, A1>, std::vector<int, A2>>;
3030
ASSERT_SAME_TYPE(C::difference_type, std::ptrdiff_t);
3131
ASSERT_SAME_TYPE(C::size_type, std::size_t);
32-
C c;
32+
const C c;
3333
ASSERT_NOEXCEPT(c.max_size());
3434
ASSERT_SAME_TYPE(decltype(c.max_size()), C::size_type);
3535
assert(c.max_size() <= 10);
@@ -41,7 +41,7 @@ int main(int, char**) {
4141
using C = std::flat_map<int, int, std::less<int>, std::vector<int, A2>, std::vector<int, A1>>;
4242
ASSERT_SAME_TYPE(C::difference_type, std::ptrdiff_t);
4343
ASSERT_SAME_TYPE(C::size_type, std::size_t);
44-
C c;
44+
const C c;
4545
ASSERT_NOEXCEPT(c.max_size());
4646
ASSERT_SAME_TYPE(decltype(c.max_size()), C::size_type);
4747
assert(c.max_size() <= 10);
@@ -53,7 +53,7 @@ int main(int, char**) {
5353
ASSERT_SAME_TYPE(C::difference_type, std::ptrdiff_t);
5454
ASSERT_SAME_TYPE(C::size_type, std::size_t);
5555
const C::size_type max_dist = static_cast<C::size_type>(std::numeric_limits<C::difference_type>::max());
56-
C c;
56+
const C c;
5757
ASSERT_NOEXCEPT(c.max_size());
5858
ASSERT_SAME_TYPE(decltype(c.max_size()), C::size_type);
5959
assert(c.max_size() <= max_dist);
@@ -64,7 +64,7 @@ int main(int, char**) {
6464
ASSERT_SAME_TYPE(C::difference_type, std::ptrdiff_t);
6565
ASSERT_SAME_TYPE(C::size_type, std::size_t);
6666
const C::size_type max_dist = static_cast<C::size_type>(std::numeric_limits<C::difference_type>::max());
67-
C c;
67+
const C c;
6868
ASSERT_NOEXCEPT(c.max_size());
6969
ASSERT_SAME_TYPE(decltype(c.max_size()), C::size_type);
7070
assert(c.max_size() <= max_dist);

libcxx/test/std/containers/container.adaptors/flat.map/flat.map.capacity/size.pass.cpp

Lines changed: 27 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -20,12 +20,37 @@
2020

2121
int main(int, char**) {
2222
{
23-
using M = std::flat_map<int, char>;
24-
M m = {{1, 'a'}, {1, 'b'}, {4, 'd'}, {5, 'e'}, {5, 'h'}};
23+
using M = std::flat_map<int, char>;
24+
const M m = {{1, 'a'}, {1, 'b'}, {4, 'd'}, {5, 'e'}, {5, 'h'}};
2525
ASSERT_SAME_TYPE(decltype(m.size()), std::size_t);
2626
ASSERT_NOEXCEPT(m.size());
2727
assert(m.size() == 3);
2828
}
29+
{
30+
using M = std::flat_map<int, char>;
31+
const M m = {{1, 'a'}};
32+
ASSERT_SAME_TYPE(decltype(m.size()), std::size_t);
33+
ASSERT_NOEXCEPT(m.size());
34+
assert(m.size() == 1);
35+
}
36+
{
37+
using M = std::flat_map<int, char>;
38+
const M m;
39+
ASSERT_SAME_TYPE(decltype(m.size()), std::size_t);
40+
ASSERT_NOEXCEPT(m.size());
41+
assert(m.size() == 0);
42+
}
43+
{
44+
using M = std::flat_map<int, char>;
45+
M m;
46+
std::size_t s = 1000000;
47+
for (auto i = 0u; i < s; ++i) {
48+
m.emplace(i, 'a');
49+
}
50+
ASSERT_SAME_TYPE(decltype(m.size()), std::size_t);
51+
ASSERT_NOEXCEPT(m.size());
52+
assert(m.size() == s);
53+
}
2954

3055
return 0;
3156
}

libcxx/test/std/containers/container.adaptors/flat.map/flat.map.cons/alloc.pass.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,7 @@ int main(int, char**) {
4444
static_assert(!std::is_constructible_v<M3, const A2&>);
4545
}
4646
{
47-
//explicit
47+
// explicit
4848
using M =
4949
std::flat_map<int,
5050
long,

0 commit comments

Comments
 (0)