我有这样的 capnproto 定义:
struct School {
name @0 :Text;
address @1 :Address;
foundation @2 :Date;
emailAddresses @3 :List(Text);
}
我想使用类似于此的代码在构建器中设置 emailAddresses 字段(但这不会编译):
static School::Builder random_School() {
capnp::MallocMessageBuilder msg;
School::Builder result = msg.initRoot<School>();
result.setName(rand_str(36));
result.setAddress(random_Address());
result.setFoundation(random_Date());
result.initEmailAddresses(item_count);
for (size_t i = 0; i < item_count; ++i) {
result.getEmailAddresses()[i] = rand_str(37); // rand_str returns std::string
}
return result;
}
这样做的正确方法是什么?