Plain old data structure (POD)

Source: Internet
Author: User

Plain old data structure, abbreviated as POD, is a type of data structure defined in the C ++ language standard. pods are suitable for systems that require explicit underlying data operations. Pods are usually used at the boundaries of the system, that is, different systems can only interact with each other in the form of underlying data, and the high-level logic of the system cannot be compatible with each other. For example, if the field value of an object is constructed from external data, the system cannot perform semantic check and interpretation on the object. Then, pods are used to store data.

 

Definition

The pod types include the following C ++ types, their CV-Qualified types, and their base-type array types:

  • Scalar type)
  • Pod class type)

 

Scalar type

Terms scalar types include the following C ++ categories and their CV-Qualified types:

  • Arithmetic type (arithmetic type)
  • Enumeration type)
  • Pointer type)
  • Pointer to member type (pointer-to-member type)

 

The term arithmetic type includes the following types of C ++:

  • Integer type)
  • Floating type)

 

The term integer type includes the following types of C ++:

  • Signed Char, short, Int, long ),
  • Unsigned integer (unsigned char, unsigned short, unsigned int, unsigned long ),
  • Char and wchar_t
  • Boolean bool.
  • The floating point type includes the float, double, and long double types of C ++.

 

The term Enumeration type includes various enumeration types, that is, the set of named constant values (named constant values.

The term pointer type includes the following types of C ++:

  • Null Pointer pointer-to-void (void *),
  • Object Pointer pointer-to-object and pointer-to-static-member-data pointing to static data members (both shapes are T *, where T is the object type ),
  • The function pointer-to-function and the pointer-to-static-member-function pointing to the static member function (both form T (*)(...), T is the type of the return value of the function ).

 

The term pointer to the member type includes the following C ++ types:

  • Pointer to a non-static data member (pointer-to-nonstatic-member-data), such as t C: * Indicates a pointer to a data member whose type is t in Class C;
  • Pointer to a non-static member function (pointer-to-nonstatic-member-functions), such as T (C ::*)(...) pointer to a member function whose return value type is T.

 

Pod type

Pod classes refer to aggregate classes (pod-struct types) and pod-Union types (pod-Union types), and do not have the following members:

  • Pointer to a non-static data member (including an array) of the member type ).
  • Non-static data members (including arrays) of the non-pod type ).
  • The reference type is a non-static data member.
  • User-Defined copy and value assignment operators.
  • User-Defined destructor.


Term aggregation refers to any array or class and does not have the following features:

  • User-Defined constructor.
  • Private or protected non-static data member.
  • Base class.
  • Virtual functions.


It can be seen that the pod class type refers to class, struct, union, and does not have user-defined constructor, destructor, copy operator, and assignment operator. It does not have an inheritance relationship, therefore, there is no base class; there is no virtual function, so there is no virtual table; non-static data members with no private or protected attributes, no reference type, and no non-pod class type (that is, nested classes must all be POD), no pointer to the member type (because this type contains the this pointer ).

 

Purpose

The pod type is very important when the source code is compatible with ansi c. The pod object and the corresponding objects in C language share some common features, including initialization, replication, memory layout, and addressing.

An example is the object initialization in the new expression of c ++ below. The difference between POD and non-pod: Non-pod objects or arrays are always initialized; pod objects or arrays may not be initialized.

Other pod-related C ++ features:

  • Memory Layout-the pod object consists of consecutive bytes.
  • Initialization -- for non-const pod objects, if there is no initialization Declaration, there is an uncertain Initial Value (indeterminate initial value ). the default initialization value of the pod object is 0. static pod object initialization is a given initial value. If it is a local static pod object, it is initialized before it enters the scope. For non-local static pod objects, the initial value is assigned before any dynamic initialization.
  • Copy-a pod object can be directly copied (for example, memcpy () to an array of other characters or an object of the same pod type to keep its value unchanged. The pod type can be used as a character of the string class of the standard template. For this reason, if the return value of the function is non-pod type, the return value of the function cannot be passed through registers.
  • Addressing-the address of a pod object can be an address constant expression. A reference to a pod member can be a reference constant expression. A pointer to a pod-struct object, suitable for converting to its initial value with reinterpret_cast.

 

 1 #include <iostream> 2 using namespace std; 3  4 /// POD 5 template<typename T> 6 struct POD 7 { 8     static const bool Result=false; 9 };10 template<>struct POD<bool>{static const bool Result=true;};11 template<>struct POD<signed char>{static const bool Result=true;};12 template<>struct POD<unsigned char>{static const bool Result=true;};13 template<>struct POD<signed short>{static const bool Result=true;};14 template<>struct POD<unsigned short>{static const bool Result=true;};15 template<>struct POD<signed int>{static const bool Result=true;};16 template<>struct POD<unsigned int>{static const bool Result=true;};17 template<>struct POD<signed long long>{static const bool Result=true;};18 template<>struct POD<unsigned long long>{static const bool Result=true;};19 template<>struct POD<char>{static const bool Result=true;};20 template<>struct POD<wchar_t>{static const bool Result=true;};21 template<typename T>struct POD<T*>{static const bool Result=true;};22 template<typename T>struct POD<T&>{static const bool Result=true;};23 template<typename T, typename C>struct POD<T C::*>{static const bool Result=true;};24 template<typename T, int _Size>struct POD<T[_Size]>{static const bool Result=POD<T>::Result;};25 template<typename T>struct POD<const T>{static const bool Result=POD<T>::Result;};26 template<typename T>struct POD<volatile T>{static const bool Result=POD<T>::Result;};27 template<typename T>struct POD<const volatile T>{static const bool Result=POD<T>::Result;};28 29 /// Test Def30 class MyClass{int a,b;};31 class MyPOD{int a,b;};32 typedef int* PTI;33 typedef int ARR[10];34 typedef int& BAS;35 typedef const int CON;36 typedef std::pair<int,int> PII;37 typedef std::pair<MyClass,MyPOD> PMM;38 template<>struct POD<MyPOD>{static const bool Result=true;};39 template<typename K, typename V>struct POD< std::pair<K, V> >{static const bool Result=POD<K>::Result && POD<V>::Result;};40 41 int main()42 {43     /// Test Code44     cout << POD<int>::Result << endl;45     cout << POD<PTI>::Result << endl;46     cout << POD<ARR>::Result << endl;47     cout << POD<BAS>::Result << endl;48     cout << POD<CON>::Result << endl;49     cout << POD<MyClass>::Result << endl;50     cout << POD<MyPOD>::Result << endl;51     cout << POD<PII>::Result << endl;52     cout << POD<PMM>::Result << endl;53     return 0;54 }

 

Plain old data structure (POD)

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.