From 371d4e0b7972a59e5cff418c44e4043c016ce56a Mon Sep 17 00:00:00 2001 From: Luca Longinotti Date: Fri, 1 Mar 2019 16:35:29 +0100 Subject: [PATCH] Fix compile error with a vector of non-std::strings. CreateVectorOfStrings() expects a vector of std::string types, but that's not always the case. --- src/idl_gen_cpp.cpp | 8 +++++++- tests/monster_test_generated.h | 4 ++-- 2 files changed, 9 insertions(+), 3 deletions(-) diff --git a/src/idl_gen_cpp.cpp b/src/idl_gen_cpp.cpp index 12f63f64d..b79c7f219 100644 --- a/src/idl_gen_cpp.cpp +++ b/src/idl_gen_cpp.cpp @@ -2341,7 +2341,13 @@ class CppGenerator : public BaseGenerator { auto vector_type = field.value.type.VectorType(); switch (vector_type.base_type) { case BASE_TYPE_STRING: { - code += "_fbb.CreateVectorOfStrings(" + value + ")"; + // Use by-function serialization to emulate CreateVectorOfStrings(); + // this works also with non-std strings. + code += "_fbb.CreateVector> "; + code += "(" + value + ".size(), "; + code += "[](size_t i, _VectorArgs *__va) { "; + code += "return __va->__fbb->CreateString(__va->_" + value + "[i]);"; + code += " }, &_va )"; break; } case BASE_TYPE_STRUCT: { diff --git a/tests/monster_test_generated.h b/tests/monster_test_generated.h index 9154439eb..29cbe3963 100644 --- a/tests/monster_test_generated.h +++ b/tests/monster_test_generated.h @@ -2489,7 +2489,7 @@ inline flatbuffers::Offset CreateMonster(flatbuffers::FlatBufferBuilder auto _test_type = _o->test.type; auto _test = _o->test.Pack(_fbb); auto _test4 = _o->test4.size() ? _fbb.CreateVectorOfStructs(_o->test4) : 0; - auto _testarrayofstring = _o->testarrayofstring.size() ? _fbb.CreateVectorOfStrings(_o->testarrayofstring) : 0; + auto _testarrayofstring = _o->testarrayofstring.size() ? _fbb.CreateVector> (_o->testarrayofstring.size(), [](size_t i, _VectorArgs *__va) { return __va->__fbb->CreateString(__va->__o->testarrayofstring[i]); }, &_va ) : 0; auto _testarrayoftables = _o->testarrayoftables.size() ? _fbb.CreateVector> (_o->testarrayoftables.size(), [](size_t i, _VectorArgs *__va) { return CreateMonster(*__va->__fbb, __va->__o->testarrayoftables[i].get(), __va->__rehasher); }, &_va ) : 0; auto _enemy = _o->enemy ? CreateMonster(_fbb, _o->enemy.get(), _rehasher) : 0; auto _testnestedflatbuffer = _o->testnestedflatbuffer.size() ? _fbb.CreateVector(_o->testnestedflatbuffer) : 0; @@ -2507,7 +2507,7 @@ inline flatbuffers::Offset CreateMonster(flatbuffers::FlatBufferBuilder auto _testf = _o->testf; auto _testf2 = _o->testf2; auto _testf3 = _o->testf3; - auto _testarrayofstring2 = _o->testarrayofstring2.size() ? _fbb.CreateVectorOfStrings(_o->testarrayofstring2) : 0; + auto _testarrayofstring2 = _o->testarrayofstring2.size() ? _fbb.CreateVector> (_o->testarrayofstring2.size(), [](size_t i, _VectorArgs *__va) { return __va->__fbb->CreateString(__va->__o->testarrayofstring2[i]); }, &_va ) : 0; auto _testarrayofsortedstruct = _o->testarrayofsortedstruct.size() ? _fbb.CreateVectorOfStructs(_o->testarrayofsortedstruct) : 0; auto _flex = _o->flex.size() ? _fbb.CreateVector(_o->flex) : 0; auto _test5 = _o->test5.size() ? _fbb.CreateVectorOfStructs(_o->test5) : 0;