c++flatbuffers

Can I call CreateString inline when creating a table?


Assuming following table in flatbuffers:

table Person {
  id:int32
  name:string;
  age:int16;
  location:string;
}

Then in c++ code, are both of these approaches correct?

//Calling CreateString inline
auto person = CreatePerson(builder,
                           10,
                           builder.CreateString(name),
                           25,
                           builder.CreateString("New York"));

vs

auto name = builder.CreateString("John");
auto loc = builder.CreateString("New York");

auto person = CreatePerson(builder, 10, name, 25, loc);

Reason I'm asking is because we're supposed to create all offsets before creating the table but documentation doesn't clearly state if inline CreateString call is acceptable?


Solution

  • Both codes are valid. The argument evaluation order is unimportant. builder just serializes single values at free spaces in a byte array. CreatePerson(builder, 10, name, 25, loc) copies serialized data from builder.