About the pod type in C ++

Source: Internet
Author: User
Tags traits

 

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

Related Article

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.