diff --git a/src/idl_gen_cpp.cpp b/src/idl_gen_cpp.cpp index facd7d286..fa2708000 100644 --- a/src/idl_gen_cpp.cpp +++ b/src/idl_gen_cpp.cpp @@ -234,11 +234,16 @@ static void GenEnum(const Parser &parser, EnumDef &enum_def, // underlying type to the interface type. std::string GenUnderlyingCast(const Parser &parser, const FieldDef &field, bool from, const std::string &val) { - return (field.value.type.enum_def && IsScalar(field.value.type.base_type)) || - field.value.type.base_type == BASE_TYPE_BOOL - ? "static_cast<" + GenTypeBasic(parser, field.value.type, from) + ">(" + - val + ")" - : val; + if (from && field.value.type.base_type == BASE_TYPE_BOOL) { + return val + " != 0"; + } else if ((field.value.type.enum_def && + IsScalar(field.value.type.base_type)) || + field.value.type.base_type == BASE_TYPE_BOOL) { + return "static_cast<" + GenTypeBasic(parser, field.value.type, from) + + ">(" + val + ")"; + } else { + return val; + } } std::string GenFieldOffsetName(const FieldDef &field) { diff --git a/tests/monster_test_generated.h b/tests/monster_test_generated.h index 90814a94f..14780bf2d 100644 --- a/tests/monster_test_generated.h +++ b/tests/monster_test_generated.h @@ -236,7 +236,7 @@ struct Monster FLATBUFFERS_FINAL_CLASS : private flatbuffers::Table { const MyGame::Example::Monster *testnestedflatbuffer_nested_root() const { return flatbuffers::GetRoot(testnestedflatbuffer()->Data()); } const Stat *testempty() const { return GetPointer(VT_TESTEMPTY); } Stat *mutable_testempty() { return GetPointer(VT_TESTEMPTY); } - bool testbool() const { return static_cast(GetField(VT_TESTBOOL, 0)); } + bool testbool() const { return GetField(VT_TESTBOOL, 0) != 0; } bool mutate_testbool(bool _testbool) { return SetField(VT_TESTBOOL, static_cast(_testbool)); } int32_t testhashs32_fnv1() const { return GetField(VT_TESTHASHS32_FNV1, 0); } bool mutate_testhashs32_fnv1(int32_t _testhashs32_fnv1) { return SetField(VT_TESTHASHS32_FNV1, _testhashs32_fnv1); }