From 80d111712f4948897bba20b29db0eabd7f5cad3d Mon Sep 17 00:00:00 2001 From: John Wellbelove Date: Thu, 9 Jun 2016 21:15:53 +0100 Subject: [PATCH] Only destruct the content if the new value is of a different type. --- src/variant.h | 17 ++++++++++++++--- 1 file changed, 14 insertions(+), 3 deletions(-) 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; }