Identify a type in generic portable mode in C ++

Is there a way to identify the type in a generic and portable way for serialization? I know that std::type_index

both std::type_info

are platform dependent and cannot be used for serialization.

One way would be to make a map between std::type_index

and one "type information" and serialize the "type information", but this will slow down serialization.

Is there a standard way to do this?

PS: I want to learn how to do efficient serialization, I don't want to use a library.

+3


source to share


2 answers


If you want to transfer your objects from one process to another, and this process will live at different times and on different machines, then what you are looking at is more messenging than serialization .

There are effective mailing libraries: Google Protobuf or Apache Thrift are good examples.



The basic idea is to supplant the message definition to make it agnostic (and compiler) and then generate code bindings from that definition. Then, from your code, you create a message in memory, serialize that message (xml / json / binary), and then on another platform deserialize the message to another representation in memory.

If that sounds like an extra step, well, it is. On the other hand, this means that the message format is no longer tied to the internal implementation (indirection layer ...), and therefore you get the ability to change your internal implementation as you see fit, while still being able to read / write the message across different versions (for interaction with older versions of the program version, for example).

+1


source


Is there a way to identify the type in a generic and portable way for serialization?

This is possible in a general but not portable manner.



The string returned from std::type_info::name()

is often a malformed type name that uniquely identifies the type. This name can be used to identify the type of serialized objects.

The name mangling differs from the compiler by the compiler, so a program compiled with a different compiler may not read serialized objects.

+1


source







All Articles