diff --git a/samples/monster_generated.h b/samples/monster_generated.h index e1a1e7e1c..fb261849c 100644 --- a/samples/monster_generated.h +++ b/samples/monster_generated.h @@ -78,8 +78,8 @@ struct EquipmentUnion { type(Equipment_NONE), value(nullptr) { std::swap(type, u.type); std::swap(value, u.value); } EquipmentUnion(const EquipmentUnion &) FLATBUFFERS_NOEXCEPT; - EquipmentUnion &operator=(EquipmentUnion u) FLATBUFFERS_NOEXCEPT - { std::swap(type, u.type); std::swap(value, u.value); return *this; } + EquipmentUnion &operator=(const EquipmentUnion &u) FLATBUFFERS_NOEXCEPT + { EquipmentUnion t(u); std::swap(type, t.type); std::swap(value, t.value); return *this; } EquipmentUnion &operator=(EquipmentUnion &&u) FLATBUFFERS_NOEXCEPT { std::swap(type, u.type); std::swap(value, u.value); return *this; } ~EquipmentUnion() { Reset(); } diff --git a/src/idl_gen_cpp.cpp b/src/idl_gen_cpp.cpp index 983e01628..48751f405 100644 --- a/src/idl_gen_cpp.cpp +++ b/src/idl_gen_cpp.cpp @@ -688,8 +688,8 @@ class CppGenerator : public BaseGenerator { code_ += " type({{NONE}}), value(nullptr)"; code_ += " { std::swap(type, u.type); std::swap(value, u.value); }"; code_ += " {{NAME}}Union(const {{NAME}}Union &) FLATBUFFERS_NOEXCEPT;"; - code_ += " {{NAME}}Union &operator=({{NAME}}Union u) FLATBUFFERS_NOEXCEPT"; - code_ += " { std::swap(type, u.type); std::swap(value, u.value); return *this; }"; + code_ += " {{NAME}}Union &operator=(const {{NAME}}Union &u) FLATBUFFERS_NOEXCEPT"; + code_ += " { {{NAME}}Union t(u); std::swap(type, t.type); std::swap(value, t.value); return *this; }"; code_ += " {{NAME}}Union &operator=({{NAME}}Union &&u) FLATBUFFERS_NOEXCEPT"; code_ += " { std::swap(type, u.type); std::swap(value, u.value); return *this; }"; code_ += " ~{{NAME}}Union() { Reset(); }"; diff --git a/tests/monster_test_generated.h b/tests/monster_test_generated.h index 1974a84ab..10aecc809 100644 --- a/tests/monster_test_generated.h +++ b/tests/monster_test_generated.h @@ -109,8 +109,8 @@ struct AnyUnion { type(Any_NONE), value(nullptr) { std::swap(type, u.type); std::swap(value, u.value); } AnyUnion(const AnyUnion &) FLATBUFFERS_NOEXCEPT; - AnyUnion &operator=(AnyUnion u) FLATBUFFERS_NOEXCEPT - { std::swap(type, u.type); std::swap(value, u.value); return *this; } + AnyUnion &operator=(const AnyUnion &u) FLATBUFFERS_NOEXCEPT + { AnyUnion t(u); std::swap(type, t.type); std::swap(value, t.value); return *this; } AnyUnion &operator=(AnyUnion &&u) FLATBUFFERS_NOEXCEPT { std::swap(type, u.type); std::swap(value, u.value); return *this; } ~AnyUnion() { Reset(); } diff --git a/tests/union_vector/union_vector_generated.h b/tests/union_vector/union_vector_generated.h index 7eaa0fb07..104c66589 100644 --- a/tests/union_vector/union_vector_generated.h +++ b/tests/union_vector/union_vector_generated.h @@ -56,8 +56,8 @@ struct CharacterUnion { type(Character_NONE), value(nullptr) { std::swap(type, u.type); std::swap(value, u.value); } CharacterUnion(const CharacterUnion &) FLATBUFFERS_NOEXCEPT; - CharacterUnion &operator=(CharacterUnion u) FLATBUFFERS_NOEXCEPT - { std::swap(type, u.type); std::swap(value, u.value); return *this; } + CharacterUnion &operator=(const CharacterUnion &u) FLATBUFFERS_NOEXCEPT + { CharacterUnion t(u); std::swap(type, t.type); std::swap(value, t.value); return *this; } CharacterUnion &operator=(CharacterUnion &&u) FLATBUFFERS_NOEXCEPT { std::swap(type, u.type); std::swap(value, u.value); return *this; } ~CharacterUnion() { Reset(); }