diff --git a/src/idl_gen_cpp.cpp b/src/idl_gen_cpp.cpp index b94c2f7b0..623ada2f1 100644 --- a/src/idl_gen_cpp.cpp +++ b/src/idl_gen_cpp.cpp @@ -2284,11 +2284,6 @@ class CppGenerator : public BaseGenerator { code_ += " memset(this, 0, sizeof({{STRUCT_NAME}}));"; code_ += " }"; - // Generate a copy constructor. - code_ += " {{STRUCT_NAME}}(const {{STRUCT_NAME}} &_o) {"; - code_ += " memcpy(this, &_o, sizeof({{STRUCT_NAME}}));"; - code_ += " }"; - // Generate a constructor that takes all fields as arguments. std::string arg_list; std::string init_list; diff --git a/tests/monster_test_generated.h b/tests/monster_test_generated.h index c9b1572dd..14a2ae82c 100644 --- a/tests/monster_test_generated.h +++ b/tests/monster_test_generated.h @@ -197,9 +197,6 @@ MANUALLY_ALIGNED_STRUCT(2) Test FLATBUFFERS_FINAL_CLASS { Test() { memset(this, 0, sizeof(Test)); } - Test(const Test &_o) { - memcpy(this, &_o, sizeof(Test)); - } Test(int16_t _a, int8_t _b) : a_(flatbuffers::EndianScalar(_a)), b_(flatbuffers::EndianScalar(_b)), @@ -237,9 +234,6 @@ MANUALLY_ALIGNED_STRUCT(16) Vec3 FLATBUFFERS_FINAL_CLASS { Vec3() { memset(this, 0, sizeof(Vec3)); } - Vec3(const Vec3 &_o) { - memcpy(this, &_o, sizeof(Vec3)); - } Vec3(float _x, float _y, float _z, double _test1, Color _test2, const Test &_test3) : x_(flatbuffers::EndianScalar(_x)), y_(flatbuffers::EndianScalar(_y)), @@ -302,9 +296,6 @@ MANUALLY_ALIGNED_STRUCT(4) Ability FLATBUFFERS_FINAL_CLASS { Ability() { memset(this, 0, sizeof(Ability)); } - Ability(const Ability &_o) { - memcpy(this, &_o, sizeof(Ability)); - } Ability(uint32_t _id, uint32_t _distance) : id_(flatbuffers::EndianScalar(_id)), distance_(flatbuffers::EndianScalar(_distance)) { diff --git a/tests/test.cpp b/tests/test.cpp index e4145bd22..e2a3e1f66 100644 --- a/tests/test.cpp +++ b/tests/test.cpp @@ -526,6 +526,18 @@ void SizePrefixedTest() { TEST_EQ_STR(m->name()->c_str(), "bob"); } + +void TriviallyCopyableTest() { + #if __GNUG__ && __GNUC__ < 5 + TEST_EQ(__has_trivial_copy(Vec3), true); + #else + #if __cplusplus >= 201103L + TEST_EQ(std::is_trivially_copyable::value, true); + #endif + #endif +} + + // example of parsing text straight into a buffer, and generating // text back from it: void ParseAndGenerateTextTest() { @@ -1809,6 +1821,9 @@ int main(int /*argc*/, const char * /*argv*/[]) { #else auto &flatbuf = flatbuf1; #endif // !defined(FLATBUFFERS_CPP98_STL) + + TriviallyCopyableTest(); + AccessFlatBufferTest(reinterpret_cast(rawbuf.c_str()), rawbuf.length()); AccessFlatBufferTest(flatbuf.data(), flatbuf.size());