flatbuffers icon indicating copy to clipboard operation
flatbuffers copied to clipboard

C++ operator== fails for native inline types

Open GMellar opened this issue 4 years ago • 5 comments

In Version v2.0.5 the generated C++ code for tables with native_inline elements fails to compile because it produces something like this (From the monster test): ((lhs.pos == rhs.pos) || (lhs.pos && rhs.pos && *lhs.pos == *rhs.pos))

In my case it produces the same but for a native_inline which are generated as structs. The problem does not happen in v2.0.0 so I searched the commits and it seems to be introduced in 962751a6ec4a0cf9552b1291231edfc60210e77b #6486.

GMellar avatar Dec 17 '21 18:12 GMellar

Can you make a minimal example schema (and flatc generation options) to show the issue?

dbaileychess avatar Dec 23 '21 15:12 dbaileychess

I ran into the same problem recently and made a minimal example in a gist.

The code is generated via flatc -c --gen-object-api --gen-compare native.fbs and fails to compile as mentioned as it tries to dereference the nat_type struct despite it not being a pointer.

Removing the gen-compare works but that defeats the purpose of course!

kaffeewolf avatar Dec 28 '21 12:12 kaffeewolf

Thanks for the example, I'll take a look this weekend. Sorry for the delay.

dbaileychess avatar Jan 14 '22 22:01 dbaileychess

Thanks for reporting!

I think something like the following would fix it (against an internal version so line numbers are off)

--- a/flatbuffers/src/idl_gen_cpp.cpp
+++ b/flatbuffers/src/idl_gen_cpp.cpp
@@ -1649,7 +1649,7 @@ class CppGenerator : public BaseGenerato
            field.value.type.element != BASE_TYPE_UTYPE)) {
         if (!compare_op.empty()) { compare_op += " &&\n      "; }
         auto accessor = Name(field) + accessSuffix;
-        if (struct_def.fixed ||
+        if (struct_def.fixed || field.native_inline ||
             field.value.type.base_type != BASE_TYPE_STRUCT) {
           compare_op += "(lhs." + accessor + " == rhs." + accessor + ")";
         } else {

Note that you would need to provide a comparison operator for your native type.

mikaraento avatar Feb 02 '22 23:02 mikaraento

@mikaraento Thanks for the suggestion!

@GMellar and @kaffeewolf Let me know how this is now. I only fixed the native_inline stuff in the example. I am not sure the native_type had any issues, but let me know.

dbaileychess avatar Feb 03 '22 06:02 dbaileychess