Commit c0f6f5fae1cc6556eee72414b769e726dd99b1bb
1 parent
236e44aa
Fixed return value from push operations
Showing
3 changed files
with
10 additions
and
18 deletions
examples11/04-vectortmpl/vector.h
@@ -138,8 +138,7 @@ public: | @@ -138,8 +138,7 @@ public: | ||
138 | // cout << "emplace_back" << endl; | 138 | // cout << "emplace_back" << endl; |
139 | resize_before_push(); | 139 | resize_before_push(); |
140 | new (data + size) C(std::forward<Args>(args)...); | 140 | new (data + size) C(std::forward<Args>(args)...); |
141 | - ++size; | ||
142 | - return *(data + size); | 141 | + return *(data + size++); |
143 | } | 142 | } |
144 | 143 | ||
145 | #if 1 | 144 | #if 1 |
@@ -147,24 +146,21 @@ public: | @@ -147,24 +146,21 @@ public: | ||
147 | // cout << "push_back with rvalue argument" << endl; | 146 | // cout << "push_back with rvalue argument" << endl; |
148 | resize_before_push(); | 147 | resize_before_push(); |
149 | new (data + size) C(std::move(s)); | 148 | new (data + size) C(std::move(s)); |
150 | -// new (data + size) C(s); | ||
151 | - ++size; | ||
152 | - return *(data + size); | 149 | + // new (data + size) C(s); |
150 | + return *(data + size++); | ||
153 | } | 151 | } |
154 | 152 | ||
155 | C &push_back(const C &s) { | 153 | C &push_back(const C &s) { |
156 | // cout << "push_back with reference argument" << endl; | 154 | // cout << "push_back with reference argument" << endl; |
157 | resize_before_push(); | 155 | resize_before_push(); |
158 | new (data + size) C(s); | 156 | new (data + size) C(s); |
159 | - ++size; | ||
160 | - return *(data + size); | 157 | + return *(data + size++); |
161 | } | 158 | } |
162 | #else | 159 | #else |
163 | template <class T> C &push_back(T &&s) { | 160 | template <class T> C &push_back(T &&s) { |
164 | resize_before_push(); | 161 | resize_before_push(); |
165 | new (data + size) C(std::forward<T>(s)); | 162 | new (data + size) C(std::forward<T>(s)); |
166 | - ++size; | ||
167 | - return *(data + size); | 163 | + return *(data + size++); |
168 | } | 164 | } |
169 | #endif | 165 | #endif |
170 | }; | 166 | }; |
examples11/05-foldexpr/testvector.cpp
@@ -22,8 +22,8 @@ main () | @@ -22,8 +22,8 @@ main () | ||
22 | a.push_back(8); | 22 | a.push_back(8); |
23 | a.push_back(9); | 23 | a.push_back(9); |
24 | a.push_back(10); | 24 | a.push_back(10); |
25 | - a.push_back(11); | ||
26 | - a.push_back(12,13,14); | 25 | + cout << a.push_back(11) << endl; |
26 | + cout << a.push_back(12,13,14) << endl; | ||
27 | int arg = 15; | 27 | int arg = 15; |
28 | a.push_back(arg); | 28 | a.push_back(arg); |
29 | b=a; | 29 | b=a; |
examples11/05-foldexpr/vector.h
@@ -138,23 +138,19 @@ public: | @@ -138,23 +138,19 @@ public: | ||
138 | // cout << "emplace_back" << endl; | 138 | // cout << "emplace_back" << endl; |
139 | resize_before_push(); | 139 | resize_before_push(); |
140 | new (data + size) C(std::forward<Args>(args)...); | 140 | new (data + size) C(std::forward<Args>(args)...); |
141 | - ++size; | ||
142 | - return *(data + size); | 141 | + return *(data + size++); |
143 | } | 142 | } |
144 | 143 | ||
145 | private: | 144 | private: |
146 | template <class T> C &push_back_single(T &&s) { | 145 | template <class T> C &push_back_single(T &&s) { |
147 | resize_before_push(); | 146 | resize_before_push(); |
148 | new (data + size) C(std::forward<T>(s)); | 147 | new (data + size) C(std::forward<T>(s)); |
149 | - ++size; | ||
150 | - return *(data + size); | 148 | + return *(data + size++); |
151 | } | 149 | } |
152 | -public: | ||
153 | 150 | ||
151 | +public: | ||
154 | template <class... Args> C &push_back(Args &&... args) { | 152 | template <class... Args> C &push_back(Args &&... args) { |
155 | return (push_back_single(std::forward<Args>(args)), ...); | 153 | return (push_back_single(std::forward<Args>(args)), ...); |
156 | } | 154 | } |
157 | - | ||
158 | - | ||
159 | }; | 155 | }; |
160 | #endif /* __VECTOR_H__ */ | 156 | #endif /* __VECTOR_H__ */ |