ソースを参照

Fixing bug that would cause crashes in maps when moved

Sam Jaffe 5 年 前
コミット
02132d4e29
1 ファイル変更16 行追加6 行削除
  1. 16 6
      variant.hpp

+ 16 - 6
variant.hpp

@@ -113,20 +113,30 @@ public:
   
   variant(const variant<Ts...>& old) : type_id(old.type_id), data()
   {
-    helper_t::copy(num_types - old.type_id - 1, &old.data, &data);
+    helper_t::copy(num_types - type_id - 1, &old.data, &data);
   }
   
   variant(variant<Ts...>&& old) : type_id(old.type_id), data()
   {
-    helper_t::move(num_types - old.type_id - 1, &old.data, &data);
+    helper_t::move(num_types - type_id - 1, &old.data, &data);
+    old.type_id = invalid_type();
   }
   
   // Serves as both the move and the copy asignment operator.
-  variant<Ts...>& operator= (variant<Ts...> old)
+  variant<Ts...>& operator= (variant<Ts...> const &old)
   {
-    std::swap(type_id, old.type_id);
-    std::swap(data, old.data);
-    
+    helper_t::destroy(num_types - type_id - 1, &data);
+    type_id = old.type_id;
+    helper_t::copy(num_types - type_id - 1, &old.data, &data);
+    return *this;
+  }
+
+  variant<Ts...>& operator= (variant<Ts...> &&old)
+  {
+    helper_t::destroy(num_types - type_id - 1, &data);
+    type_id = old.type_id;
+    helper_t::move(num_types - type_id - 1, &old.data, &data);
+    old.type_id = invalid_type();
     return *this;
   }