diff --git a/src/variant.h b/src/variant.h index a7fe2118..a5e05b0d 100644 --- a/src/variant.h +++ b/src/variant.h @@ -726,9 +726,20 @@ namespace etl { STATIC_ASSERT(Type_Is_Supported::value, "Unsupported type"); - destruct_current(); - new(static_cast(data)) T(value); - type_id = Type_Id_Lookup::type_id; + // Assigning the same type as last time? + if (type_id == Type_Id_Lookup::type_id) + { + // Do a simple copy. + *static_cast(data) = value; + } + else + { + // We must destruct the old type, as the new one is different. + destruct_current(); + new(static_cast(data)) T(value); + type_id = Type_Id_Lookup::type_id; + } + return *this; }