Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

"Non-pointer POD" type in C++

Tags:

c++

types

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?

like image 206
PPenguin Avatar asked Jan 24 '18 14:01

PPenguin


1 Answers

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 tell1.

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.

like image 156
Bartek Banachewicz Avatar answered Oct 24 '22 20:10

Bartek Banachewicz