From ece9eabf65a214e5c05b0831c5d7842addbc73e0 Mon Sep 17 00:00:00 2001 From: tzik Date: Fri, 17 Jan 2025 22:12:18 +0900 Subject: [PATCH] [C++] Use C++17 inline variables for enum_value --- src/idl_gen_cpp.cpp | 14 ++++++-- .../generated_cpp17/monster_test_generated.h | 32 +++++++++---------- .../generated_cpp17/union_vector_generated.h | 12 +++---- 3 files changed, 34 insertions(+), 24 deletions(-) diff --git a/src/idl_gen_cpp.cpp b/src/idl_gen_cpp.cpp index ba158ecb593..f9f7f74747b 100644 --- a/src/idl_gen_cpp.cpp +++ b/src/idl_gen_cpp.cpp @@ -1355,7 +1355,12 @@ class CppGenerator : public BaseGenerator { } auto value = GetEnumValUse(enum_def, ev); - code_ += " static const {{ENUM_NAME}} enum_value = " + value + ";"; + if (opts_.g_cpp_std >= cpp::CPP_STD_17) { + code_ += + " static inline const {{ENUM_NAME}} enum_value = " + value + ";"; + } else { + code_ += " static const {{ENUM_NAME}} enum_value = " + value + ";"; + } code_ += "};"; code_ += ""; } @@ -1390,7 +1395,12 @@ class CppGenerator : public BaseGenerator { } auto value = GetEnumValUse(enum_def, ev); - code_ += " static const {{ENUM_NAME}} enum_value = " + value + ";"; + if (opts_.g_cpp_std >= cpp::CPP_STD_17) { + code_ += + " static inline const {{ENUM_NAME}} enum_value = " + value + ";"; + } else { + code_ += " static const {{ENUM_NAME}} enum_value = " + value + ";"; + } code_ += "};"; code_ += ""; } diff --git a/tests/cpp17/generated_cpp17/monster_test_generated.h b/tests/cpp17/generated_cpp17/monster_test_generated.h index 68c49af6716..103684f4882 100644 --- a/tests/cpp17/generated_cpp17/monster_test_generated.h +++ b/tests/cpp17/generated_cpp17/monster_test_generated.h @@ -236,35 +236,35 @@ inline const char *EnumNameAny(Any e) { } template struct AnyTraits { - static const Any enum_value = Any::NONE; + static inline const Any enum_value = Any::NONE; }; template<> struct AnyTraits { - static const Any enum_value = Any::Monster; + static inline const Any enum_value = Any::Monster; }; template<> struct AnyTraits { - static const Any enum_value = Any::TestSimpleTableWithEnum; + static inline const Any enum_value = Any::TestSimpleTableWithEnum; }; template<> struct AnyTraits { - static const Any enum_value = Any::MyGame_Example2_Monster; + static inline const Any enum_value = Any::MyGame_Example2_Monster; }; template struct AnyUnionTraits { - static const Any enum_value = Any::NONE; + static inline const Any enum_value = Any::NONE; }; template<> struct AnyUnionTraits { - static const Any enum_value = Any::Monster; + static inline const Any enum_value = Any::Monster; }; template<> struct AnyUnionTraits { - static const Any enum_value = Any::TestSimpleTableWithEnum; + static inline const Any enum_value = Any::TestSimpleTableWithEnum; }; template<> struct AnyUnionTraits { - static const Any enum_value = Any::MyGame_Example2_Monster; + static inline const Any enum_value = Any::MyGame_Example2_Monster; }; struct AnyUnion { @@ -363,35 +363,35 @@ inline const char *EnumNameAnyUniqueAliases(AnyUniqueAliases e) { } template struct AnyUniqueAliasesTraits { - static const AnyUniqueAliases enum_value = AnyUniqueAliases::NONE; + static inline const AnyUniqueAliases enum_value = AnyUniqueAliases::NONE; }; template<> struct AnyUniqueAliasesTraits { - static const AnyUniqueAliases enum_value = AnyUniqueAliases::M; + static inline const AnyUniqueAliases enum_value = AnyUniqueAliases::M; }; template<> struct AnyUniqueAliasesTraits { - static const AnyUniqueAliases enum_value = AnyUniqueAliases::TS; + static inline const AnyUniqueAliases enum_value = AnyUniqueAliases::TS; }; template<> struct AnyUniqueAliasesTraits { - static const AnyUniqueAliases enum_value = AnyUniqueAliases::M2; + static inline const AnyUniqueAliases enum_value = AnyUniqueAliases::M2; }; template struct AnyUniqueAliasesUnionTraits { - static const AnyUniqueAliases enum_value = AnyUniqueAliases::NONE; + static inline const AnyUniqueAliases enum_value = AnyUniqueAliases::NONE; }; template<> struct AnyUniqueAliasesUnionTraits { - static const AnyUniqueAliases enum_value = AnyUniqueAliases::M; + static inline const AnyUniqueAliases enum_value = AnyUniqueAliases::M; }; template<> struct AnyUniqueAliasesUnionTraits { - static const AnyUniqueAliases enum_value = AnyUniqueAliases::TS; + static inline const AnyUniqueAliases enum_value = AnyUniqueAliases::TS; }; template<> struct AnyUniqueAliasesUnionTraits { - static const AnyUniqueAliases enum_value = AnyUniqueAliases::M2; + static inline const AnyUniqueAliases enum_value = AnyUniqueAliases::M2; }; struct AnyUniqueAliasesUnion { diff --git a/tests/cpp17/generated_cpp17/union_vector_generated.h b/tests/cpp17/generated_cpp17/union_vector_generated.h index 0ef405c0b63..0a22f80b49d 100644 --- a/tests/cpp17/generated_cpp17/union_vector_generated.h +++ b/tests/cpp17/generated_cpp17/union_vector_generated.h @@ -195,27 +195,27 @@ inline const char *EnumNameGadget(Gadget e) { } template struct GadgetTraits { - static const Gadget enum_value = Gadget::NONE; + static inline const Gadget enum_value = Gadget::NONE; }; template<> struct GadgetTraits { - static const Gadget enum_value = Gadget::FallingTub; + static inline const Gadget enum_value = Gadget::FallingTub; }; template<> struct GadgetTraits { - static const Gadget enum_value = Gadget::HandFan; + static inline const Gadget enum_value = Gadget::HandFan; }; template struct GadgetUnionTraits { - static const Gadget enum_value = Gadget::NONE; + static inline const Gadget enum_value = Gadget::NONE; }; template<> struct GadgetUnionTraits { - static const Gadget enum_value = Gadget::FallingTub; + static inline const Gadget enum_value = Gadget::FallingTub; }; template<> struct GadgetUnionTraits { - static const Gadget enum_value = Gadget::HandFan; + static inline const Gadget enum_value = Gadget::HandFan; }; struct GadgetUnion {