[Clang] Don't form a type constraint if the concept is invalid (#122065)
[llvm-project.git] / libcxx / test / std / containers / container.adaptors / NaiveStaticVector.h
blob61fa3504e34e3a2c4c755bf92b92e43b629c3655
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 //===----------------------------------------------------------------------===//
9 #ifndef SUPPORT_NAIVE_STATIC_VECTOR_H
10 #define SUPPORT_NAIVE_STATIC_VECTOR_H
12 #include <cstddef>
13 #include <utility>
14 #include "test_iterators.h"
15 #include "test_macros.h"
17 template <class T, std::size_t N>
18 struct NaiveStaticVector {
19 struct CapacityError {};
21 using value_type = T;
22 using difference_type = short;
23 using size_type = unsigned short;
24 using iterator = random_access_iterator<T*>;
25 using const_iterator = random_access_iterator<const T*>;
27 explicit NaiveStaticVector() = default;
28 template <class It>
29 explicit NaiveStaticVector(It first, It last) {
30 while (first != last)
31 insert(*first++);
34 // Moving-from a NaiveStaticVector leaves the source vector holding moved-from objects.
35 // This is intentional (the "Naive" in the name).
36 // Specifically, moving-out-of a sorted+uniqued NaiveStaticVector<MoveOnly>
37 // will leave it in a non-sorted+uniqued state.
39 NaiveStaticVector(const NaiveStaticVector&) = default;
40 NaiveStaticVector(NaiveStaticVector&&) = default; // deliberately don't reset size_
41 NaiveStaticVector& operator=(const NaiveStaticVector&) = default;
42 NaiveStaticVector& operator=(NaiveStaticVector&&) = default;
44 iterator begin() { return iterator(data_); }
45 const_iterator begin() const { return const_iterator(data_); }
46 const_iterator cbegin() const { return const_iterator(data_); }
47 iterator end() { return begin() + size(); }
48 const_iterator end() const { return begin() + size(); }
49 size_type size() const { return size_; }
50 bool empty() const { return size_ == 0; }
52 void clear() { size_ = 0; }
54 template <class It>
55 iterator insert(const_iterator pos, It first, It last) {
56 iterator result = pos - cbegin() + begin();
57 while (first != last) {
58 insert(pos++, *first++);
60 return result;
63 iterator insert(const_iterator pos, T value) {
64 if (size_ == N) {
65 throw CapacityError();
67 int i = pos - cbegin();
68 size_ += 1;
69 std::move_backward(&data_[i], &data_[size_ - 1], &data_[size_]);
70 data_[i] = std::move(value);
71 return begin() + i;
74 template <class... Args>
75 iterator emplace(const_iterator pos, Args&&... args) {
76 return insert(pos, T(std::forward<Args>(args)...));
79 iterator erase(const_iterator first, const_iterator last) {
80 int i = first - cbegin();
81 int j = last - cbegin();
82 std::move(&data_[j], &data_[size_], &data_[i]);
83 size_ -= (last - first);
84 return begin() + i;
87 iterator erase(const_iterator pos) { return erase(pos, std::next(pos)); }
89 private:
90 T data_[N];
91 std::size_t size_ = 0;
94 #endif // SUPPORT_NAIVE_STATIC_VECTOR_H