An idiot with a rib
Links: https://www.zhihu.com/question/36379130/answer/69853366
Source: Know
Copyright belongs to the author. Commercial reprint please contact the author for authorization, non-commercial reprint please specify the source.
POD, full name plain old Data,plain means it is a normal type, old means it can be compatible with C, and can be manipulated using the most primitive functions of C such as memcpy ().
The pod is divided into two basic concepts in the c++11.That
Ordinary (trival) and standard layout.
- The first is the trivial (trival) definition, usually an ordinary class or struct that needs to meet the following definitions:
- Have trivial default constructors and destructors. The default meaning is automatically generated by the compiler for us, not allowed to be defined by ourselves, but because C++11 provides default, it can also be defined by their own =default, such as
struct trival{
Trival () {}=default;
}
is to satisfy this requirement, and
struct notrival{
Notrival () {};
}
This requirement is not fulfilled (even if there is nothing in the body of the constructor we define).
This requirement is not tied to a constructor with parameters. You can customize the constructor with parameters.
- Have trivial copy constructors and move constructors. The default meaning is ditto, and you can use =default.
- Has a trivial copy assignment operator and a move assignment operator.
- Cannot contain virtual functions and virtual base classes.
2. Next is the definition of the standard layout:
- All non-static members have the same access level ( the access level is public,private,protected),
struct t1{
Private:
int A;
Public
int b;
The standard layout is not satisfied because a, B access level is different.
- In class and struct inheritance, you need to meet one of the following two scenarios:
- There are non-static classes in a derived class, the derived class can have only one base class that contains only static members.
- The base class has non-static members, and non-static members are not allowed in the derived class.
(
These two sentences look quite around the mouth, in fact, is to illustrate a fact, about the fact that non-static data, derived classes have non-static data then its base class can only be static, and the base class can only have one. If the base class has non-static, then the derived class cannot have non-static. There is a seesaw feeling, non-static opposite sitting is static, father and son class is sitting on the seesaw at both ends of this correspondence relationship. )
- The first non-static type in a class is not of the same type as the base class. Like what
struct a:b{
b b;
int C;
} is not eligible for this condition. Because the first member of a is of the base class B type.
- No virtual classes and virtual base classes (duplicates in trival)
- All non-static data members conform to the requirements of the standard layout, which is actually a recursive definition.
so in c++11, Pod is the two aspects of satisfying the ordinary (trival) and standard layout. You can use the Is_pod<t>::value in <type_traits> to determine if T is a pod type.
Having said so much, then why do we need the pod to satisfy the condition of the data?
- You can use byte assignments, such as memset,memcpy operations
- Compatible with C memory layouts.
- This ensures that static initialization is safe and effective.
C + + code style guidelines: POD