Commit cee544e6e420282beb477da0ca219fd05a1adaab

Authored by Grzegorz Jabłoński
1 parent 0ce72890

Added emplace_back

examples11/04-vectortmpl/vector.h
@@ -134,10 +134,37 @@ public: @@ -134,10 +134,37 @@ public:
134 return o; 134 return o;
135 } 135 }
136 136
137 - void push_back(const C &s) { 137 + template <class... Args> C &emplace_back(Args &&... args) {
  138 + // cout << "emplace_back" << endl;
  139 + resize_before_push();
  140 + new (data + size) C(std::forward<Args>(args)...);
  141 + ++size;
  142 + return *(data + size);
  143 + }
  144 +
  145 +#if 1
  146 + C &push_back(C &&s) {
  147 + // cout << "push_back with rvalue argument" << endl;
  148 + resize_before_push();
  149 + new (data + size) C(std::move(s));
  150 + ++size;
  151 + return *(data + size);
  152 + }
  153 +
  154 + C &push_back(const C &s) {
  155 + // cout << "push_back with reference argument" << endl;
138 resize_before_push(); 156 resize_before_push();
139 new (data + size) C(s); 157 new (data + size) C(s);
140 ++size; 158 ++size;
  159 + return *(data + size);
  160 + }
  161 +#else
  162 + template <class T> C &push_back(T &&s) {
  163 + resize_before_push();
  164 + new (data + size) C(std::forward<T>(s));
  165 + ++size;
  166 + return *(data + size);
141 } 167 }
  168 +#endif
142 }; 169 };
143 #endif /* __VECTOR_H__ */ 170 #endif /* __VECTOR_H__ */