diff --git a/src/idl_gen_cpp.cpp b/src/idl_gen_cpp.cpp index ce10a4e46..a8dba2a6f 100644 --- a/src/idl_gen_cpp.cpp +++ b/src/idl_gen_cpp.cpp @@ -1652,15 +1652,38 @@ class CppGenerator : public BaseGenerator { "> " : GenTypeNativePtr(cpp_type->constant, &field, false)) : type + " "); + // Generate default member initializers for >= C++11. + std::string field_di = ""; + if (opts_.g_cpp_std >= cpp::CPP_STD_11) { + field_di = "{}"; + auto native_default = field.attributes.Lookup("native_default"); + // Scalar types get parsed defaults, raw pointers get nullptrs. + if (IsScalar(field.value.type.base_type)) { + field_di = + " = " + (native_default ? std::string(native_default->constant) + : GetDefaultScalarValue(field, true)); + } else if (field.value.type.base_type == BASE_TYPE_STRUCT) { + if (IsStruct(field.value.type) && native_default) { + field_di = " = " + native_default->constant; + } + } + } code_.SetValue("FIELD_TYPE", full_type); code_.SetValue("FIELD_NAME", Name(field)); - code_ += " {{FIELD_TYPE}}{{FIELD_NAME}};"; + code_.SetValue("FIELD_DI", field_di); + code_ += " {{FIELD_TYPE}}{{FIELD_NAME}}{{FIELD_DI}};"; } } // Generate the default constructor for this struct. Properly initialize all // scalar members with default values. void GenDefaultConstructor(const StructDef &struct_def) { + code_.SetValue("NATIVE_NAME", + NativeName(Name(struct_def), &struct_def, opts_)); + // In >= C++11, default member initializers are generated. + if (opts_.g_cpp_std >= cpp::CPP_STD_11) { + return; + } std::string initializer_list; for (auto it = struct_def.fields.vec.begin(); it != struct_def.fields.vec.end(); ++it) { @@ -1698,8 +1721,6 @@ class CppGenerator : public BaseGenerator { initializer_list = "\n : " + initializer_list; } - code_.SetValue("NATIVE_NAME", - NativeName(Name(struct_def), &struct_def, opts_)); code_.SetValue("INIT_LIST", initializer_list); code_ += " {{NATIVE_NAME}}(){{INIT_LIST}} {"; diff --git a/tests/cpp17/generated_cpp17/monster_test_generated.h b/tests/cpp17/generated_cpp17/monster_test_generated.h index 654fc19de..9e181585c 100644 --- a/tests/cpp17/generated_cpp17/monster_test_generated.h +++ b/tests/cpp17/generated_cpp17/monster_test_generated.h @@ -625,8 +625,6 @@ FLATBUFFERS_STRUCT_END(Ability, 8); struct InParentNamespaceT : public flatbuffers::NativeTable { typedef InParentNamespace TableType; - InParentNamespaceT() { - } }; struct InParentNamespace FLATBUFFERS_FINAL_CLASS : private flatbuffers::Table { @@ -677,8 +675,6 @@ namespace Example2 { struct MonsterT : public flatbuffers::NativeTable { typedef Monster TableType; - MonsterT() { - } }; struct Monster FLATBUFFERS_FINAL_CLASS : private flatbuffers::Table { @@ -731,10 +727,7 @@ namespace Example { struct TestSimpleTableWithEnumT : public flatbuffers::NativeTable { typedef TestSimpleTableWithEnum TableType; - MyGame::Example::Color color; - TestSimpleTableWithEnumT() - : color(MyGame::Example::Color::Green) { - } + MyGame::Example::Color color = MyGame::Example::Color::Green; }; struct TestSimpleTableWithEnum FLATBUFFERS_FINAL_CLASS : private flatbuffers::Table { @@ -798,13 +791,9 @@ flatbuffers::Offset CreateTestSimpleTableWithEnum(flatb struct StatT : public flatbuffers::NativeTable { typedef Stat TableType; - std::string id; - int64_t val; - uint16_t count; - StatT() - : val(0), - count(0) { - } + std::string id{}; + int64_t val = 0; + uint16_t count = 0; }; struct Stat FLATBUFFERS_FINAL_CLASS : private flatbuffers::Table { @@ -908,10 +897,7 @@ flatbuffers::Offset CreateStat(flatbuffers::FlatBufferBuilder &_fbb, const struct ReferrableT : public flatbuffers::NativeTable { typedef Referrable TableType; - uint64_t id; - ReferrableT() - : id(0) { - } + uint64_t id = 0; }; struct Referrable FLATBUFFERS_FINAL_CLASS : private flatbuffers::Table { @@ -981,72 +967,51 @@ flatbuffers::Offset CreateReferrable(flatbuffers::FlatBufferBuilder struct MonsterT : public flatbuffers::NativeTable { typedef Monster TableType; - std::unique_ptr pos; - int16_t mana; - int16_t hp; - std::string name; - std::vector inventory; - MyGame::Example::Color color; - MyGame::Example::AnyUnion test; - std::vector test4; - std::vector testarrayofstring; - std::vector> testarrayoftables; - std::unique_ptr enemy; - std::vector testnestedflatbuffer; - std::unique_ptr testempty; - bool testbool; - int32_t testhashs32_fnv1; - uint32_t testhashu32_fnv1; - int64_t testhashs64_fnv1; - uint64_t testhashu64_fnv1; - int32_t testhashs32_fnv1a; - Stat *testhashu32_fnv1a; - int64_t testhashs64_fnv1a; - uint64_t testhashu64_fnv1a; - std::vector testarrayofbools; - float testf; - float testf2; - float testf3; - std::vector testarrayofstring2; - std::vector testarrayofsortedstruct; - std::vector flex; - std::vector test5; - std::vector vector_of_longs; - std::vector vector_of_doubles; - std::unique_ptr parent_namespace_test; - std::vector> vector_of_referrables; - ReferrableT *single_weak_reference; - std::vector vector_of_weak_references; - std::vector> vector_of_strong_referrables; - ReferrableT *co_owning_reference; - std::vector> vector_of_co_owning_references; - ReferrableT *non_owning_reference; - std::vector vector_of_non_owning_references; - MyGame::Example::AnyUniqueAliasesUnion any_unique; - MyGame::Example::AnyAmbiguousAliasesUnion any_ambiguous; - std::vector vector_of_enums; - MyGame::Example::Race signed_enum; - MonsterT() - : mana(150), - hp(100), - color(MyGame::Example::Color::Blue), - testbool(false), - testhashs32_fnv1(0), - testhashu32_fnv1(0), - testhashs64_fnv1(0), - testhashu64_fnv1(0), - testhashs32_fnv1a(0), - testhashu32_fnv1a(nullptr), - testhashs64_fnv1a(0), - testhashu64_fnv1a(0), - testf(3.14159f), - testf2(3.0f), - testf3(0.0f), - single_weak_reference(nullptr), - co_owning_reference(nullptr), - non_owning_reference(nullptr), - signed_enum(MyGame::Example::Race::None) { - } + std::unique_ptr pos{}; + int16_t mana = 150; + int16_t hp = 100; + std::string name{}; + std::vector inventory{}; + MyGame::Example::Color color = MyGame::Example::Color::Blue; + MyGame::Example::AnyUnion test{}; + std::vector test4{}; + std::vector testarrayofstring{}; + std::vector> testarrayoftables{}; + std::unique_ptr enemy{}; + std::vector testnestedflatbuffer{}; + std::unique_ptr testempty{}; + bool testbool = false; + int32_t testhashs32_fnv1 = 0; + uint32_t testhashu32_fnv1 = 0; + int64_t testhashs64_fnv1 = 0; + uint64_t testhashu64_fnv1 = 0; + int32_t testhashs32_fnv1a = 0; + Stat *testhashu32_fnv1a = nullptr; + int64_t testhashs64_fnv1a = 0; + uint64_t testhashu64_fnv1a = 0; + std::vector testarrayofbools{}; + float testf = 3.14159f; + float testf2 = 3.0f; + float testf3 = 0.0f; + std::vector testarrayofstring2{}; + std::vector testarrayofsortedstruct{}; + std::vector flex{}; + std::vector test5{}; + std::vector vector_of_longs{}; + std::vector vector_of_doubles{}; + std::unique_ptr parent_namespace_test{}; + std::vector> vector_of_referrables{}; + ReferrableT *single_weak_reference = nullptr; + std::vector vector_of_weak_references{}; + std::vector> vector_of_strong_referrables{}; + ReferrableT *co_owning_reference = nullptr; + std::vector> vector_of_co_owning_references{}; + ReferrableT *non_owning_reference = nullptr; + std::vector vector_of_non_owning_references{}; + MyGame::Example::AnyUniqueAliasesUnion any_unique{}; + MyGame::Example::AnyAmbiguousAliasesUnion any_ambiguous{}; + std::vector vector_of_enums{}; + MyGame::Example::Race signed_enum = MyGame::Example::Race::None; }; /// an example documentation comment: "monster object" @@ -1932,30 +1897,18 @@ flatbuffers::Offset CreateMonster(flatbuffers::FlatBufferBuilder &_fbb, struct TypeAliasesT : public flatbuffers::NativeTable { typedef TypeAliases TableType; - int8_t i8; - uint8_t u8; - int16_t i16; - uint16_t u16; - int32_t i32; - uint32_t u32; - int64_t i64; - uint64_t u64; - float f32; - double f64; - std::vector v8; - std::vector vf64; - TypeAliasesT() - : i8(0), - u8(0), - i16(0), - u16(0), - i32(0), - u32(0), - i64(0), - u64(0), - f32(0.0f), - f64(0.0) { - } + int8_t i8 = 0; + uint8_t u8 = 0; + int16_t i16 = 0; + uint16_t u16 = 0; + int32_t i32 = 0; + uint32_t u32 = 0; + int64_t i64 = 0; + uint64_t u64 = 0; + float f32 = 0.0f; + double f64 = 0.0; + std::vector v8{}; + std::vector vf64{}; }; struct TypeAliases FLATBUFFERS_FINAL_CLASS : private flatbuffers::Table {