Commit c4956ef982b8b78d66001cab79bddf18dfdcc313

Authored by Grzegorz Jabłoński
1 parent 45c0f87a

Conditional std::move depending on noexcept

examples11/04-vectortmpl/vector.h
@@ -38,8 +38,11 @@ private: @@ -38,8 +38,11 @@ private:
38 unsigned i; 38 unsigned i;
39 try { 39 try {
40 for (i = 0; i < size; i++) 40 for (i = 0; i < size; i++)
41 -// new (newdata + i) C(std::move(data[i]));  
42 - new (newdata + i) C(data[i]); 41 + if constexpr (noexcept(C(std::move(data[i])))) {
  42 + // cout << "noexcept" << endl;
  43 + new (newdata + i) C(std::move(data[i]));
  44 + } else
  45 + new (newdata + i) C(data[i]);
43 } catch (...) { 46 } catch (...) {
44 destroy_array(newdata, i); 47 destroy_array(newdata, i);
45 throw; 48 throw;
examples11/05-foldexpr/vector.h
@@ -38,8 +38,11 @@ private: @@ -38,8 +38,11 @@ private:
38 unsigned i; 38 unsigned i;
39 try { 39 try {
40 for (i = 0; i < size; i++) 40 for (i = 0; i < size; i++)
41 - new (newdata + i) C(data[i]);  
42 -// new (newdata + i) C(std::move(data[i])); 41 + if constexpr (noexcept(C(std::move(data[i]))))
  42 + new (newdata + i) C(std::move(data[i]));
  43 + else
  44 + new (newdata + i) C(data[i]);
  45 +
43 } catch (...) { 46 } catch (...) {
44 destroy_array(newdata, i); 47 destroy_array(newdata, i);
45 throw; 48 throw;
examples11/06-template_metaprogramming/vector.h
@@ -36,8 +36,11 @@ private: @@ -36,8 +36,11 @@ private:
36 unsigned i; 36 unsigned i;
37 try { 37 try {
38 for (i = 0; i < size; i++) 38 for (i = 0; i < size; i++)
39 - new (newdata + i) C(data[i]);  
40 -// new (newdata + i) C(std::move(data[i])); 39 + if constexpr (noexcept(C(std::move(data[i]))))
  40 + new (newdata + i) C(std::move(data[i]));
  41 + else
  42 + new (newdata + i) C(data[i]);
  43 +
41 } catch (...) { 44 } catch (...) {
42 destroy_array(newdata, i); 45 destroy_array(newdata, i);
43 throw; 46 throw;
@@ -148,7 +151,6 @@ public: @@ -148,7 +151,6 @@ public:
148 } 151 }
149 152
150 private: 153 private:
151 -  
152 public: 154 public:
153 void reserve(unsigned capacity) { 155 void reserve(unsigned capacity) {
154 if (capacity > std::numeric_limits<decltype(size)>::max() / (2 * sizeof(C))) 156 if (capacity > std::numeric_limits<decltype(size)>::max() / (2 * sizeof(C)))