Portability of binary serialization of double/float type in C++

后端 未结 9 2172
余生分开走
余生分开走 2020-11-27 15:50

The C++ standard does not discuss the underlying layout of float and double types, only the range of values they should represent. (This is also true for signed types, is i

9条回答
  •  独厮守ぢ
    2020-11-27 16:05

    Create an appropriate serializer/de-serializer interface for writing/reading this.

    The interface can then have several implementations and you can test your options.

    As said before, obvious options would be:

    • IEEE754 which writes / reads the binary chunk if directly supported by the architecture or parses it if not supported by the architecture
    • Text: always needs to parse.
    • Whatever you else you can think of.

    Just remember - once you have this layer, you can always start with IEEE754 if you only support platforms that use this format internally. This way you'll have the additional effort only when you need to support a different platform! Don't do work you don't have to.

提交回复
热议问题