From 37f04d31a3d0a1cba6a6a9a42c64a0cbce13fb25 Mon Sep 17 00:00:00 2001 From: Justin Davis Date: Sat, 13 Dec 2025 21:10:24 -0500 Subject: [PATCH 1/4] add non const overload --- src/idl_gen_cpp.cpp | 6 ++++++ 1 file changed, 6 insertions(+) diff --git a/src/idl_gen_cpp.cpp b/src/idl_gen_cpp.cpp index f056727a1f4..8c82a1e9812 100644 --- a/src/idl_gen_cpp.cpp +++ b/src/idl_gen_cpp.cpp @@ -2548,6 +2548,12 @@ class CppGenerator : public BaseGenerator { code_ += " int KeyCompareWithValue(const char *_{{FIELD_NAME}}) const {"; code_ += " return strcmp({{FIELD_NAME}}()->c_str(), _{{FIELD_NAME}});"; code_ += " }"; + // Crate an explicit overload for char*. + code_ += " int KeyCompareWithValue(char *_{{FIELD_NAME}}) const {"; + code_ += + " return KeyCompareWithValue(static_cast(_{{FIELD_NAME}}));"; + code_ += " }"; // Compares key against any string-like object (e.g. std::string_view or // std::string) that implements operator< comparison with const char*. code_ += " template"; From 518b9776dceb1b4dac62ee1a9d0548c8155da6b9 Mon Sep 17 00:00:00 2001 From: Justin Davis Date: Sat, 13 Dec 2025 21:10:28 -0500 Subject: [PATCH 2/4] update generated code --- include/flatbuffers/reflection_generated.h | 21 +++++++++++++++++++ .../generated_cpp17/monster_test_generated.h | 3 +++ tests/monster_test_generated.h | 3 +++ .../ext_only/monster_test_generated.hpp | 3 +++ .../filesuffix_only/monster_test_suffix.h | 3 +++ .../monster_test_suffix.hpp | 3 +++ 6 files changed, 36 insertions(+) diff --git a/include/flatbuffers/reflection_generated.h b/include/flatbuffers/reflection_generated.h index 9a9ed6b9b2b..bd39b04f1e0 100644 --- a/include/flatbuffers/reflection_generated.h +++ b/include/flatbuffers/reflection_generated.h @@ -275,6 +275,9 @@ struct KeyValue FLATBUFFERS_FINAL_CLASS : private ::flatbuffers::Table { int KeyCompareWithValue(const char *_key) const { return strcmp(key()->c_str(), _key); } + int KeyCompareWithValue(char *_key) const { + return KeyCompareWithValue(static_cast(_key)); + } template int KeyCompareWithValue(const StringType& _key) const { if (key()->c_str() < _key) return -1; @@ -473,6 +476,9 @@ struct Enum FLATBUFFERS_FINAL_CLASS : private ::flatbuffers::Table { int KeyCompareWithValue(const char *_name) const { return strcmp(name()->c_str(), _name); } + int KeyCompareWithValue(char *_name) const { + return KeyCompareWithValue(static_cast(_name)); + } template int KeyCompareWithValue(const StringType& _name) const { if (name()->c_str() < _name) return -1; @@ -632,6 +638,9 @@ struct Field FLATBUFFERS_FINAL_CLASS : private ::flatbuffers::Table { int KeyCompareWithValue(const char *_name) const { return strcmp(name()->c_str(), _name); } + int KeyCompareWithValue(char *_name) const { + return KeyCompareWithValue(static_cast(_name)); + } template int KeyCompareWithValue(const StringType& _name) const { if (name()->c_str() < _name) return -1; @@ -857,6 +866,9 @@ struct Object FLATBUFFERS_FINAL_CLASS : private ::flatbuffers::Table { int KeyCompareWithValue(const char *_name) const { return strcmp(name()->c_str(), _name); } + int KeyCompareWithValue(char *_name) const { + return KeyCompareWithValue(static_cast(_name)); + } template int KeyCompareWithValue(const StringType& _name) const { if (name()->c_str() < _name) return -1; @@ -1016,6 +1028,9 @@ struct RPCCall FLATBUFFERS_FINAL_CLASS : private ::flatbuffers::Table { int KeyCompareWithValue(const char *_name) const { return strcmp(name()->c_str(), _name); } + int KeyCompareWithValue(char *_name) const { + return KeyCompareWithValue(static_cast(_name)); + } template int KeyCompareWithValue(const StringType& _name) const { if (name()->c_str() < _name) return -1; @@ -1139,6 +1154,9 @@ struct Service FLATBUFFERS_FINAL_CLASS : private ::flatbuffers::Table { int KeyCompareWithValue(const char *_name) const { return strcmp(name()->c_str(), _name); } + int KeyCompareWithValue(char *_name) const { + return KeyCompareWithValue(static_cast(_name)); + } template int KeyCompareWithValue(const StringType& _name) const { if (name()->c_str() < _name) return -1; @@ -1265,6 +1283,9 @@ struct SchemaFile FLATBUFFERS_FINAL_CLASS : private ::flatbuffers::Table { int KeyCompareWithValue(const char *_filename) const { return strcmp(filename()->c_str(), _filename); } + int KeyCompareWithValue(char *_filename) const { + return KeyCompareWithValue(static_cast(_filename)); + } template int KeyCompareWithValue(const StringType& _filename) const { if (filename()->c_str() < _filename) return -1; diff --git a/tests/cpp17/generated_cpp17/monster_test_generated.h b/tests/cpp17/generated_cpp17/monster_test_generated.h index c056e38f49d..435d514b1e5 100644 --- a/tests/cpp17/generated_cpp17/monster_test_generated.h +++ b/tests/cpp17/generated_cpp17/monster_test_generated.h @@ -1447,6 +1447,9 @@ struct Monster FLATBUFFERS_FINAL_CLASS : private ::flatbuffers::Table { int KeyCompareWithValue(const char *_name) const { return strcmp(name()->c_str(), _name); } + int KeyCompareWithValue(char *_name) const { + return KeyCompareWithValue(static_cast(_name)); + } template int KeyCompareWithValue(const StringType& _name) const { if (name()->c_str() < _name) return -1; diff --git a/tests/monster_test_generated.h b/tests/monster_test_generated.h index 594670baa7b..c1889128cf7 100644 --- a/tests/monster_test_generated.h +++ b/tests/monster_test_generated.h @@ -1443,6 +1443,9 @@ struct Monster FLATBUFFERS_FINAL_CLASS : private ::flatbuffers::Table { int KeyCompareWithValue(const char *_name) const { return strcmp(name()->c_str(), _name); } + int KeyCompareWithValue(char *_name) const { + return KeyCompareWithValue(static_cast(_name)); + } template int KeyCompareWithValue(const StringType& _name) const { if (name()->c_str() < _name) return -1; diff --git a/tests/monster_test_suffix/ext_only/monster_test_generated.hpp b/tests/monster_test_suffix/ext_only/monster_test_generated.hpp index b3db30d3be2..a1eedb81964 100644 --- a/tests/monster_test_suffix/ext_only/monster_test_generated.hpp +++ b/tests/monster_test_suffix/ext_only/monster_test_generated.hpp @@ -1434,6 +1434,9 @@ struct Monster FLATBUFFERS_FINAL_CLASS : private ::flatbuffers::Table { int KeyCompareWithValue(const char *_name) const { return strcmp(name()->c_str(), _name); } + int KeyCompareWithValue(char *_name) const { + return KeyCompareWithValue(static_cast(_name)); + } template int KeyCompareWithValue(const StringType& _name) const { if (name()->c_str() < _name) return -1; diff --git a/tests/monster_test_suffix/filesuffix_only/monster_test_suffix.h b/tests/monster_test_suffix/filesuffix_only/monster_test_suffix.h index b3db30d3be2..a1eedb81964 100644 --- a/tests/monster_test_suffix/filesuffix_only/monster_test_suffix.h +++ b/tests/monster_test_suffix/filesuffix_only/monster_test_suffix.h @@ -1434,6 +1434,9 @@ struct Monster FLATBUFFERS_FINAL_CLASS : private ::flatbuffers::Table { int KeyCompareWithValue(const char *_name) const { return strcmp(name()->c_str(), _name); } + int KeyCompareWithValue(char *_name) const { + return KeyCompareWithValue(static_cast(_name)); + } template int KeyCompareWithValue(const StringType& _name) const { if (name()->c_str() < _name) return -1; diff --git a/tests/monster_test_suffix/monster_test_suffix.hpp b/tests/monster_test_suffix/monster_test_suffix.hpp index b3db30d3be2..a1eedb81964 100644 --- a/tests/monster_test_suffix/monster_test_suffix.hpp +++ b/tests/monster_test_suffix/monster_test_suffix.hpp @@ -1434,6 +1434,9 @@ struct Monster FLATBUFFERS_FINAL_CLASS : private ::flatbuffers::Table { int KeyCompareWithValue(const char *_name) const { return strcmp(name()->c_str(), _name); } + int KeyCompareWithValue(char *_name) const { + return KeyCompareWithValue(static_cast(_name)); + } template int KeyCompareWithValue(const StringType& _name) const { if (name()->c_str() < _name) return -1; From 9d59a8cea3f2b17e986225448c1150ea3c2ae943 Mon Sep 17 00:00:00 2001 From: Justin Davis Date: Sat, 13 Dec 2025 21:46:43 -0500 Subject: [PATCH 3/4] update additional genned file --- tests/key_field/key_field_sample_generated.h | 3 +++ 1 file changed, 3 insertions(+) diff --git a/tests/key_field/key_field_sample_generated.h b/tests/key_field/key_field_sample_generated.h index cba74688fa6..a6fe45ab8f4 100644 --- a/tests/key_field/key_field_sample_generated.h +++ b/tests/key_field/key_field_sample_generated.h @@ -598,6 +598,9 @@ struct FooTable FLATBUFFERS_FINAL_CLASS : private ::flatbuffers::Table { int KeyCompareWithValue(const char *_c) const { return strcmp(c()->c_str(), _c); } + int KeyCompareWithValue(char *_c) const { + return KeyCompareWithValue(static_cast(_c)); + } template int KeyCompareWithValue(const StringType& _c) const { if (c()->c_str() < _c) return -1; From b77711cc55afbe7dadf041e3604f7a881a7f1120 Mon Sep 17 00:00:00 2001 From: Justin Davis Date: Sun, 21 Dec 2025 21:17:32 -0500 Subject: [PATCH 4/4] add test --- tests/key_field_test.cpp | 22 ++++++++++++++++++++++ tests/key_field_test.h | 1 + tests/test.cpp | 1 + 3 files changed, 24 insertions(+) diff --git a/tests/key_field_test.cpp b/tests/key_field_test.cpp index db5a0d5d3c0..7bbc758ac9e 100644 --- a/tests/key_field_test.cpp +++ b/tests/key_field_test.cpp @@ -12,6 +12,28 @@ namespace tests { using namespace keyfield::sample; +void CallLookupByKeyOnString() { + flatbuffers::FlatBufferBuilder fbb; + const auto foo = keyfield::sample::CreateFooTable( + fbb, 1, 2, fbb.CreateString("TEST_STRING")); + fbb.Finish(foo); + uint8_t* buf = fbb.GetBufferPointer(); + auto foo_table = GetFooTable(buf); + + TEST_EQ(foo_table->KeyCompareWithValue("TEST_STRING"), 0); + TEST_NE(foo_table->KeyCompareWithValue("AAA"), 0); + + // now compare with std::string + TEST_EQ(foo_table->KeyCompareWithValue(std::string{"TEST_STRING"}), 0); + TEST_NE(foo_table->KeyCompareWithValue(std::string{"AAA"}), 0); + + // now compare with a mutable char* + char mutable_str[] = "TEST_STRING"; + TEST_EQ(foo_table->KeyCompareWithValue(mutable_str), 0); + char mutable_str2[] = "AAA"; + TEST_NE(foo_table->KeyCompareWithValue(mutable_str2), 0); +} + void FixedSizedScalarKeyInStructTest() { flatbuffers::FlatBufferBuilder fbb; std::vector bazs; diff --git a/tests/key_field_test.h b/tests/key_field_test.h index be335a4f2d5..f3730da7ece 100644 --- a/tests/key_field_test.h +++ b/tests/key_field_test.h @@ -4,6 +4,7 @@ namespace flatbuffers { namespace tests { +void CallLookupByKeyOnString(); void FixedSizedScalarKeyInStructTest(); void StructKeyInStructTest(); void NestedStructKeyInStructTest(); diff --git a/tests/test.cpp b/tests/test.cpp index c94d21592f7..3122df9270f 100644 --- a/tests/test.cpp +++ b/tests/test.cpp @@ -1829,6 +1829,7 @@ int FlatBufferTests(const std::string& tests_data_path) { JsonUnsortedArrayTest(); VectorSpanTest(); NativeInlineTableVectorTest(); + CallLookupByKeyOnString(); FixedSizedScalarKeyInStructTest(); StructKeyInStructTest(); NestedStructKeyInStructTest();