Pod type (plain old data), which I saw in Morden C ++ design for the first time. To be honest, this is indeed a good book with amazing techniques. The pod type is mentioned in this section. This type is compatible with the C language struct and is mainly used when the pod object (especially the array) is being copied, you can directly use the memcpy function to improve efficiency without calling the object's copy constructor or operator =. As mentioned in Morden C ++ design, the pod type can be extracted through the special method, but no specific method is provided, which has been bothering me for a long time.
In the analysis of STL source code Hou Jie, mentioned the pod type Extraction Method in SGI-STL, but essentially no universal. The basic method is to add a typedef to a class to identify whether the class is of the pod type or not. The instance code is as follows:
# Include <iostream>
Using namespaceSTD;
Class
{
Public:
Typedef_ True_type has_trivial_destructor;
};
Class B
{
Public:
Typedef_ False_type has_trivial_destructor;
};
Template<Class t>
Class _ traits
{
Public:
TypedefTypename t::Has_trivial_destructor;
};
Template<Class t>
Void check(T&T)
{
TypedefTypename _ traits<T> ::Has_trivial_destructor trivial_destructor;
_ Check(T,Trivial_destructor());
}
Template<Class t>
Void _ Check(T&T,_ False_type)
{
Cout<"Has non-trivial destructor ."<Endl;
}
Template<Class t>
Void _ Check(T&T,_ True_type)
{
Cout<"Has trivaial destructor ."<Endl;
}
Int main()
{
A;
B;
Check(A);
Check(B);
Return0;
}
Today, I saw a method that made me look bright. It was able to determine whether a type is a pod type during compilation. However, you cannot use this feature for Type extraction. You can only determine whether a certain type is of the pod type. If not, the compiler reports an error. The method is as follows:
Template <class T>
Struct must_be_pod
{
Union
{
T noname;
};
};
Input a class as a template parameter. If it is of the pod type, it can be placed in the Union. Otherwise, it is not of the pod type. In fact, the pod type is equivalent to an object that can be put into the Union. In C ++, an object that can be put into union is an object that itself and all its member variables do not have the default constructor.
Therefore, there seems to be no good way to extract the pod type ......