diff --git a/include/flatbuffers/flatbuffers.h b/include/flatbuffers/flatbuffers.h index c42781619..198f7efc8 100644 --- a/include/flatbuffers/flatbuffers.h +++ b/include/flatbuffers/flatbuffers.h @@ -158,11 +158,13 @@ typedef std::unique_ptr> #endif // Wrapper for uoffset_t to allow safe template specialization. +// Value is allowed to be 0 to indicate a null object (see e.g. AddOffset). template struct Offset { uoffset_t o; Offset() : o(0) {} Offset(uoffset_t _o) : o(_o) {} Offset Union() const { return Offset(o); } + bool IsNull() const { return !o; } }; inline void EndianCheck() { @@ -855,7 +857,7 @@ FLATBUFFERS_FINAL_CLASS } template void AddOffset(voffset_t field, Offset off) { - if (!off.o) return; // An offset of 0 means NULL, don't store. + if (off.IsNull()) return; // Don't store. AddElement(field, ReferTo(off.o), static_cast(0)); }