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 | 138 | // cout << "emplace_back" << endl; |
139 | 139 | resize_before_push(); |
140 | 140 | new (data + size) C(std::forward<Args>(args)...); |
141 | - ++size; | |
142 | - return *(data + size); | |
141 | + return *(data + size++); | |
143 | 142 | } |
144 | 143 | |
145 | 144 | #if 1 |
... | ... | @@ -147,24 +146,21 @@ public: |
147 | 146 | // cout << "push_back with rvalue argument" << endl; |
148 | 147 | resize_before_push(); |
149 | 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 | 153 | C &push_back(const C &s) { |
156 | 154 | // cout << "push_back with reference argument" << endl; |
157 | 155 | resize_before_push(); |
158 | 156 | new (data + size) C(s); |
159 | - ++size; | |
160 | - return *(data + size); | |
157 | + return *(data + size++); | |
161 | 158 | } |
162 | 159 | #else |
163 | 160 | template <class T> C &push_back(T &&s) { |
164 | 161 | resize_before_push(); |
165 | 162 | new (data + size) C(std::forward<T>(s)); |
166 | - ++size; | |
167 | - return *(data + size); | |
163 | + return *(data + size++); | |
168 | 164 | } |
169 | 165 | #endif |
170 | 166 | }; | ... | ... |
examples11/05-foldexpr/testvector.cpp
examples11/05-foldexpr/vector.h
... | ... | @@ -138,23 +138,19 @@ public: |
138 | 138 | // cout << "emplace_back" << endl; |
139 | 139 | resize_before_push(); |
140 | 140 | new (data + size) C(std::forward<Args>(args)...); |
141 | - ++size; | |
142 | - return *(data + size); | |
141 | + return *(data + size++); | |
143 | 142 | } |
144 | 143 | |
145 | 144 | private: |
146 | 145 | template <class T> C &push_back_single(T &&s) { |
147 | 146 | resize_before_push(); |
148 | 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 | 152 | template <class... Args> C &push_back(Args &&... args) { |
155 | 153 | return (push_back_single(std::forward<Args>(args)), ...); |
156 | 154 | } |
157 | - | |
158 | - | |
159 | 155 | }; |
160 | 156 | #endif /* __VECTOR_H__ */ | ... | ... |