C++11 pod type (Learn)

Source: Internet
Author: User

What is the pod type? Pod Full name plain old Data. In layman's words, a class or struct can keep its data intact after a binary copy, so it is a pod type. An ordinary definition1. There are trivial constructors2. There are trivial copy constructors3. There are trivial move constructors4. There is a trivial copy assignment operator5. There is a trivial move assignment operator6. There are trivial destructors7. Cannot contain virtual functions8. cannot contain virtual base class [CPP] View plaincopy on code to view the snippet derivation to my Code slice # include"StdAfx.h"#include <iostream>UsingNamespaceStdClassA {a () {}};Class B {B (b&){} };Class C {C (c&&){} };Class D {DOperator= (d&){} };Class E {EOperator= (e&&){} };Class F {~F () {}};Class G {Virtualvoid foo () =0; };Classh:g {};ClassI {};int _tmain (int argc, _tchar*Argv[]) {std::cout << std::is_trivial<a>::value << Std::endl;//There are extraordinary constructors std::cout << Std::is_trivial<b>::value << Std::endl;//There are extraordinary copy constructors Std::cout << std::is_trivial<c>::value << Std::endl;//There is an extraordinary copy assignment operator Std::cout << std::is_trivial<d>::value << Std::endl;//There is an extraordinary copy assignment operator Std::cout << std::is_trivial<e>::value << Std::endl;//There is an extraordinary move assignment operator Std::cout << std::is_trivial<f>::value << Std::endl;//There are extraordinary destructors std::cout << std::is_trivial<g>::value << Std::endl;//There are virtual functions std::cout << std::is_trivial//There are virtual base classesStd::cout << std::is_trivial<i>::value << Std::endl;//The Ordinary classSystem"Pause");Return0; The definition of the run result standard layout1All non-static members have the same access rights2There can be at most one class in the inheritance tree with non-static data members3The first non-static member of a subclass cannot be a base class type4. no virtual function5. No virtual base class6All non-static members conform to the standard layout type [CPP] view plaincopy on code to view the snippet derivation to my Code slice # include"StdAfx.h"#include <iostream>UsingNamespaceStdClassAPrivate:IntAPublic:Intb };ClassB1 {StaticIntX1; };ClassB2 {IntX2; };ClassB:B1, B2 {IntX };ClassC1 {};ClassC:C1 {C1 C;};Class D {Virtualvoid foo () =0; };Classe:d {};ClassF {A x;};int _tmain (int argc, _tchar*Argv[]) {std::cout << std::is_standard_layout<a>::value << Std::endl;//violation of definition 1. Members A and B have different access rights std::cout << Std::is_standard_layout<b>::value << Std::endl;//violation of definition 2. The inheritance tree has more than two classes (including) with non-static members std::cout << std::is_standard_layout<c>::value << Std::endl;//Violation of definition 3. The first non-static member is the base class type Std::cout << std::is_standard_layout<d>::value << Std::endl;//violation of definition 4. There are virtual functions std::cout << std::is_standard_layout<e>::value << Std::endl;//violation of definition 5. There are virtual base classes std::cout << std::is_standard_layout<f>::value << Std::endl;//Violation of definition 6. Non-static member x does not conform to the standard layout typeSystem"Pause");Return0; The use of the run results pod when a data type satisfies "trivial definition" and "standard Layout", we think of it as a pod data. You can use Std::is_pod to determine whether a type is a pod type. As stated at the beginning of the article, a pod type can be binary copy, take a look at the following example. [CPP] View plaincopy on code to see a snippet derived from my Code slice # include"StdAfx.h"#include <iostream>#include <Windows.h>UsingNamespaceStdClassAPublic:IntXDoubleY };int _tmain (int argc, _tchar*Argv[]) {if (std::is_pod<a>:: Value) {Std::cout <<"Before"<<Std::endl; A; a.x =8; A.Y =10.5 Std::cout << a.x << Std::endl; std::cout << a.y << Std::endl; size_t size = sizeof< Span style= "color: #000000;" > (a); char *p = new char[size]; memcpy (p, &a, size); A *pa = (a*) p; std::cout <<  "after "<< Std::endl; Std::cout << pa->x << Std::endl; std::cout << pa->y << std: : Endl; Delete p; } system ("Pause"); return 0; The results of the operation can be seen, after a binary copy of a pod type, the data is migrated successfully. 

C++11 pod type (learn)

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.