简体   繁体   中英

“Non-pointer POD” type in C++

Is there a term for a class/struct that is both trivial and standard-layout but also has no pointer members?

Basically I'd like to refer to "really" plain-old-data types. Data that I can grab from memory and store on disk, and read back into memory for later processing because it is nothing more than a collection of ints, characters, enums, etc.

Is there a way to test at compile time if a type is a "really" plain-old-data type?

related:
What are POD types in C++?
What are Aggregates and PODs and how/why are they special?

This can depend on semantics of the structure. I could imagine a struct having int fields being keys into some volatile temporary data store (or cache). You still shouldn't serialize those, but you need internal knowledge about that struct to be able to tell 1 .

In general, C++ lacks features for generic serialization. Making this automatic just on pointers is just a tip of the iceberg (if possibly pretty accurate in general) - it's also impossible in a generic way. C++ still has no reflection, and thus no way to check "every member" for some condition.

The realistic approaches could be:

  • preprocessing the class sources before build to scan for pointers
  • declaring all structs that are to be serialized with some macros that track the types
  • the regular template check could be implemented for a set of known names for fields

All of those have their limitations, though, and together with my earlier reservations, I'm not sure how practical they'd be.


1 This of course goes both ways; pointers could be used to store relative offsets, and thus be perfectly serializable.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM