Repository: google/flatbuffers
File: src/idl_gen_cpp.cpp (C++)

Code before:
      // use operator< of flatbuffers::String
      code_ += "    return *{{FIELD_NAME}}() < *o->{{FIELD_NAME}}();";
    } else if (is_array) {
      const auto &elem_type = field.value.type.VectorType();
      if (IsScalar(elem_type.base_type)) {
        code_ += "    return KeyCompareWithValue(o->{{FIELD_NAME}}()) < 0;";
      }
    } else {
      code_ += "    return {{FIELD_NAME}}() < o->{{FIELD_NAME}}();";
    }
    code_ += "  }";

    if (is_string) {
      code_ += "  int KeyCompareWithValue(const char *_{{FIELD_NAME}}) const {";
      code_ += "    return strcmp({{FIELD_NAME}}()->c_str(), _{{FIELD_NAME}});";
    } else if (is_array) {
      const auto &elem_type = field.value.type.VectorType();
      if (IsScalar(elem_type.base_type)) {
        std::string input_type = "flatbuffers::Array<" +
                                 GenTypeBasic(elem_type, false) + ", " +
                                 NumToString(elem_type.fixed_length) + ">";
        code_.SetValue("INPUT_TYPE", input_type);
        code_ +=
            "  int KeyCompareWithValue(const {{INPUT_TYPE}} *_{{FIELD_NAME}}"
            ") const { ";
        code_ += "    auto curr_{{FIELD_NAME}} = {{FIELD_NAME}}();";
        code_ += "    for (auto i = 0; i < curr_{{FIELD_NAME}}->size(); i++) {";
        code_ +=
            "      const auto {{FIELD_NAME}}_l = curr_{{FIELD_NAME}}->Get(i);";
        code_ += "      const auto {{FIELD_NAME}}_r = _{{FIELD_NAME}}->Get(i);";
        code_ += "      if({{FIELD_NAME}}_l != {{FIELD_NAME}}_r) ";
        code_ +=
            "        return static_cast<int>({{FIELD_NAME}}_l > "
            "{{FIELD_NAME}}_r)"
            " - static_cast<int>({{FIELD_NAME}}_l < {{FIELD_NAME}}_r);";
        code_ += "    }";
        code_ += "    return 0;";
      }
    } else {
      FLATBUFFERS_ASSERT(IsScalar(field.value.type.base_type));
      auto type = GenTypeBasic(field.value.type, false);
      if (opts_.scoped_enums && field.value.type.enum_def &&
          IsScalar(field.value.type.base_type)) {
        type = GenTypeGet(field.value.type, " ", "const ", " *", true);
      }
      // Returns {field<val: -1, field==val: 0, field>val: +1}.
      code_.SetValue("KEY_TYPE", type);
      code_ +=
          "  int KeyCompareWithValue({{KEY_TYPE}} _{{FIELD_NAME}}) const {";
      code_ +=
          "    return static_cast<int>({{FIELD_NAME}}() > _{{FIELD_NAME}}) - "

Review comment (suggestion):
I recommend using `const auto* curr_{{FIELD_NAME}} = {{FIELD_NAME}}();`

Code after:
    } else {
      FLATBUFFERS_ASSERT(IsScalar(field.value.type.base_type));
      auto type = GenTypeBasic(field.value.type, false);
      if (opts_.scoped_enums && field.value.type.enum_def &&
          IsScalar(field.value.type.base_type)) {
        type = GenTypeGet(field.value.type, " ", "const ", " *", true);
      }
      // Returns {field<val: -1, field==val: 0, field>val: +1}.
      code_.SetValue("KEY_TYPE", type);
      code_ +=
          "  int KeyCompareWithValue({{KEY_TYPE}} _{{FIELD_NAME}}) const {";
      code_ +=
          "    return static_cast<int>({{FIELD_NAME}}() > _{{FIELD_NAME}}) - "
          "static_cast<int>({{FIELD_NAME}}() < _{{FIELD_NAME}});";
    }
    code_ += "  }";
  }

  void GenTableUnionAsGetters(const FieldDef &field) {
    const auto &type = field.value.type;
    auto u = type.enum_def;

    if (!type.enum_def->uses_multiple_type_instances)
      code_ +=
          "  template<typename T> "
          "const T *{{NULLABLE_EXT}}{{FIELD_NAME}}_as() const;";

    for (auto u_it = u->Vals().begin(); u_it != u->Vals().end(); ++u_it) {
      auto &ev = **u_it;
      if (ev.union_type.base_type == BASE_TYPE_NONE) { continue; }
      auto full_struct_name = GetUnionElement(ev, false, opts_);

      // @TODO: Mby make this decisions more universal? How?
      code_.SetValue("U_GET_TYPE",
                     EscapeKeyword(Name(field) + UnionTypeFieldSuffix()));
      code_.SetValue("U_ELEMENT_TYPE", WrapInNameSpace(u->defined_namespace,
                                                       GetEnumValUse(*u, ev)));
      code_.SetValue("U_FIELD_TYPE", "const " + full_struct_name + " *");
      code_.SetValue("U_FIELD_NAME", Name(field) + "_as_" + Name(ev));
      code_.SetValue("U_NULLABLE", NullableExtension());

      // `const Type *union_name_asType() const` accessor.
      code_ += "  {{U_FIELD_TYPE}}{{U_NULLABLE}}{{U_FIELD_NAME}}() const {";
      code_ +=
          "    return {{U_GET_TYPE}}() == {{U_ELEMENT_TYPE}} ? "
          "static_cast<{{U_FIELD_TYPE}}>({{FIELD_NAME}}()) "
          ": nullptr;";
      code_ += "  }";
    }
  }