0

I'd like to understand more about these types.

Instead of listing the requirements of a type to be trivial, standard layout, etc. I'd also like to know what it means to be trivial, standard layout, etc. Based on my understanding (which could be absolutely wrong), it's as below:

Trivially copyable – may be safely copied with std::memcpy. If you memcpy the data back to an object, it would be identical with the original object.

Trivial – trivially copyable, and has one or more default constructors, all of which are either trivial or deleted, and at least one of which is not deleted (must be trivial). Meaning it is trivially copyable, and statically initialized if we do default initialization (initialized at compile time, because the compiler knows the how default constructor works)

Standard layout – The layout (order of data members represented in memory, alignment, and the memory/padding size taken) is same as in C

POD – Trivial and standard layout. This type is fully compatible with C.

Please correct any misunderstanding I have, and provide references if possible.

Also, when do we want to create objects that are trivial, trivially copyable, standard layout, POD? Examples would be appreciated.

zemageht
  • 105
  • 6

1 Answers1

0

what it means to be trivial, standard layout, etc.

Being a trivial type implies that language rules which apply to trivial types will apply to it. Same for other special categories of types. For example, as you mentioned, only trivially copyable types may be copied with memcpy.

Also, when do we want to create objects that are trivial, trivially copyable, standard layout, POD?

You mentioned that POD are compatible with C. Therefore, when you want interaction across the language barrier, you want to use types that are compatible with both languages.

In some cases copying and possibly comparison of trivial types can be more efficient than same operations of non trivial types. As such, trivial types may be preferred in cases where they offer significant improvement.

eerorika
  • 232,697
  • 12
  • 197
  • 326