1 //===----------------------------------------------------------------------===// 2 // 3 // The LLVM Compiler Infrastructure 4 // 5 // This file is dual licensed under the MIT and the University of Illinois Open 6 // Source Licenses. See LICENSE.TXT for details. 7 // 8 //===----------------------------------------------------------------------===// 9 10 // <vector> 11 12 // template <class... Args> iterator emplace(const_iterator pos, Args&&... args); 13 14 #if _LIBCPP_DEBUG2 >= 1 15 #define _LIBCPP_ASSERT(x, m) ((x) ? (void)0 : std::exit(0)) 16 #endif 17 18 #include <vector> 19 #include <cassert> 20 #include "../../../stack_allocator.h" 21 #include "../../../min_allocator.h" 22 23 #ifndef _LIBCPP_HAS_NO_RVALUE_REFERENCES 24 25 class A 26 { 27 int i_; 28 double d_; 29 30 A(const A&); 31 A& operator=(const A&); 32 public: 33 A(int i, double d) 34 : i_(i), d_(d) {} 35 36 A(A&& a) 37 : i_(a.i_), 38 d_(a.d_) 39 { 40 a.i_ = 0; 41 a.d_ = 0; 42 } 43 44 A& operator=(A&& a) 45 { 46 i_ = a.i_; 47 d_ = a.d_; 48 a.i_ = 0; 49 a.d_ = 0; 50 return *this; 51 } 52 53 int geti() const {return i_;} 54 double getd() const {return d_;} 55 }; 56 57 #endif // _LIBCPP_HAS_NO_RVALUE_REFERENCES 58 59 int main() 60 { 61 #ifndef _LIBCPP_HAS_NO_RVALUE_REFERENCES 62 { 63 std::vector<A> c; 64 std::vector<A>::iterator i = c.emplace(c.cbegin(), 2, 3.5); 65 assert(i == c.begin()); 66 assert(c.size() == 1); 67 assert(c.front().geti() == 2); 68 assert(c.front().getd() == 3.5); 69 i = c.emplace(c.cend(), 3, 4.5); 70 assert(i == c.end()-1); 71 assert(c.size() == 2); 72 assert(c.front().geti() == 2); 73 assert(c.front().getd() == 3.5); 74 assert(c.back().geti() == 3); 75 assert(c.back().getd() == 4.5); 76 i = c.emplace(c.cbegin()+1, 4, 6.5); 77 assert(i == c.begin()+1); 78 assert(c.size() == 3); 79 assert(c.front().geti() == 2); 80 assert(c.front().getd() == 3.5); 81 assert(c[1].geti() == 4); 82 assert(c[1].getd() == 6.5); 83 assert(c.back().geti() == 3); 84 assert(c.back().getd() == 4.5); 85 } 86 { 87 std::vector<A, stack_allocator<A, 7> > c; 88 std::vector<A, stack_allocator<A, 7> >::iterator i = c.emplace(c.cbegin(), 2, 3.5); 89 assert(i == c.begin()); 90 assert(c.size() == 1); 91 assert(c.front().geti() == 2); 92 assert(c.front().getd() == 3.5); 93 i = c.emplace(c.cend(), 3, 4.5); 94 assert(i == c.end()-1); 95 assert(c.size() == 2); 96 assert(c.front().geti() == 2); 97 assert(c.front().getd() == 3.5); 98 assert(c.back().geti() == 3); 99 assert(c.back().getd() == 4.5); 100 i = c.emplace(c.cbegin()+1, 4, 6.5); 101 assert(i == c.begin()+1); 102 assert(c.size() == 3); 103 assert(c.front().geti() == 2); 104 assert(c.front().getd() == 3.5); 105 assert(c[1].geti() == 4); 106 assert(c[1].getd() == 6.5); 107 assert(c.back().geti() == 3); 108 assert(c.back().getd() == 4.5); 109 } 110 #if _LIBCPP_DEBUG2 >= 1 111 { 112 std::vector<A> c1; 113 std::vector<A> c2; 114 std::vector<A>::iterator i = c1.emplace(c2.cbegin(), 2, 3.5); 115 assert(false); 116 } 117 #endif 118 #if __cplusplus >= 201103L 119 { 120 std::vector<A, min_allocator<A>> c; 121 std::vector<A, min_allocator<A>>::iterator i = c.emplace(c.cbegin(), 2, 3.5); 122 assert(i == c.begin()); 123 assert(c.size() == 1); 124 assert(c.front().geti() == 2); 125 assert(c.front().getd() == 3.5); 126 i = c.emplace(c.cend(), 3, 4.5); 127 assert(i == c.end()-1); 128 assert(c.size() == 2); 129 assert(c.front().geti() == 2); 130 assert(c.front().getd() == 3.5); 131 assert(c.back().geti() == 3); 132 assert(c.back().getd() == 4.5); 133 i = c.emplace(c.cbegin()+1, 4, 6.5); 134 assert(i == c.begin()+1); 135 assert(c.size() == 3); 136 assert(c.front().geti() == 2); 137 assert(c.front().getd() == 3.5); 138 assert(c[1].geti() == 4); 139 assert(c[1].getd() == 6.5); 140 assert(c.back().geti() == 3); 141 assert(c.back().getd() == 4.5); 142 } 143 #if _LIBCPP_DEBUG2 >= 1 144 { 145 std::vector<A, min_allocator<A>> c1; 146 std::vector<A, min_allocator<A>> c2; 147 std::vector<A, min_allocator<A>>::iterator i = c1.emplace(c2.cbegin(), 2, 3.5); 148 assert(false); 149 } 150 #endif 151 #endif 152 #endif // _LIBCPP_HAS_NO_RVALUE_REFERENCES 153 } 154