"Dried Foods" C + + through template-specific implementation of type extraction instances--implementation of distinguishing between basic types and custom types memcpy

Source: Internet
Author: User

Type extraction is a common programming technique designed to implement different types of data to the same function, such as the implementation of cout in STL, which differs from class encapsulation in that we do not know what type of object we call, the type extraction is the compiler knows the type, first implementation, The encapsulation of a class is the first definition of the type, and then the implementation method. Here we can use the special template to implement its programming ideas.

We take memcpy as an example, when we copy the basic type, we only use the data on the pointer passed by the copy, if it is a string type, we need to open up space on the heap, if the passed pointer is copied directly, then there is It is possible (the implementation of the string type under VS is that if the string is not long, it is saved as an array, and if the string is too long, then the space on the heap is saved by the pointer) , a common error such as two times of destruction occurs.

Here we need to define two classes in a header file to differentiate whether it is a basic type and the code is as follows:

struct __truetype//base type {bool Get () {return true;}}; struct __falsetype//non-basic type {bool Get () {return false;}};

Second, define the class template, make the basic type special, non-basic type does not need to be special:

template <class _tp>//non-basic types of struct typetraits{typedef __falsetype    __IsPODType;}; template <>//basic types of all-special struct typetraits< bool>{typedef __truetype      __IsPODType;}; template <>struct typetraits< char>{typedef __truetype      __IsPODType;}; Template <>struct typetraits< unsigned char >{typedef __truetype      __IsPODType;}; template <>struct typetraits< short>{typedef __truetype      __IsPODType;}; Template <>struct typetraits< unsigned short >{typedef __truetype      __IsPODType;}; template <>struct typetraits< int>{typedef __truetype      __IsPODType;}; Template <>struct typetraits< unsigned int >{typedef __TrueType     __IsPODType;}; template <>struct typetraits< long>{typedef __truetype      __IsPODType;}; Template <>struct typetraits< unsigned long >{typedef __truetype      __IsPODType;}; template <>struct typetraits< long long >{typedef __truetype      __IsPODType;}; Template <>struct typetraits< unsigned long long>{typedef __truetype      __IsPODType;}; template <>struct typetraits< float>{typedef __truetype      __IsPODType;}; template <>struct typetraits< double>{typedef __truetype      __IsPODType;}; Template <>struct typetraits< long double >{typedef __truetype     __ispodtype;}; template <class _tp>struct typetraits< _tp*>//the pointer type of the bias {Typedef __TrueType      __IsPODType;};

To do this we also need to memcpy the implementation of:

Template <class t>void Copy (const t* SRC, t* DST, size_t size) {//cout << "__truetype:" << typeid (T). Name () << endl;//test with if (Typetraits <t>::__ispodtype (). Get ())//return value is true (is the base type) then call memcpy{memcpy (DST, SRC, size*sizeof (T));} Else{for (size_t i = 0; i < size; ++i)/* is not a base type, the overload implementation of the assignment operator memcpy*/{dst[i] = Src[i];}}

If there is any shortage, I hope to criticize.

This article is from the "Pawnsir It Road" blog, so be sure to keep this source http://10743407.blog.51cto.com/10733407/1751557

"Dried Foods" C + + through template-specific implementation of type extraction instances--implementation of distinguishing between basic types and custom types memcpy

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.