Flatbuffers:我可以将 int 字段更改为具有 1 个 int 的结构吗?

Flatbuffers: can I change int field to struct with 1 int?

基于 flatbuffers 的主要贡献者提出的针对 null 字段的非常好的方法:

https://github.com/google/flatbuffers/issues/333#issuecomment-155856289

The easiest way to get a null default for an integer field is to wrap it in a struct. This will get you null if scalar isn't present. It also doesn't take up any more space on the wire than a regular int.

struct myint { x:int; }
table mytable { scalar:myint; }enter code here

this will get you null if scalar isn't present. It also doesn't take up any more space on the wire than a regular int.

同样基于 flatbuffers 文档:

https://google.github.io/flatbuffers/md__schemas.html

You can't change types of fields once they're used, with the exception of same-size data where a reinterpret_cast would give you a desirable result, e.g. you could change a uint to an int if no values in current data use the high bit yet.

我的问题是我可以将 int 视为 reinterpret_cast-能够 myint?

换句话说,如果我从一个简单的 int 作为字段开始,我可以稍后决定我真的希望这个 int 可以为 null 并将其更改为 myint 吗?我知道在第一个 int 模式中曾经是 default value 的所有值在 myint 模式中都将被读取为 null 并且我可以接受那。

当然,明显的后续问题是我可以对所有标量类型做同样的事情吗?

虽然没有明确记录,但 intmyint 是线格式兼容的(它们都是内联存储的)。就像你说的,你将失去任何默认值实例成为 null.