Trivial or standard layout versus POD

In layman's terms, what's the difference between trivial types, standard layout types, and POD?

In particular, I want to determine if new T is different from new T() for any parameter of the T template. Which of the characteristics like is_trivial , is_standard_layout and is_pod should is_pod choose?

(As a side question, is it possible to implement any of these features of this type without compiler magic?)

+58
c ++ constructor initialization typetraits pod
Jun 27 '11 at 17:25
source share
2 answers

I do not think that this can be done truly unprofessional, at least without unnecessary explanations. One of the important points is static or dynamic initialization, but the explanation is that the layman will have several pages on their own ...

PODs were (incorrectly) defined in C ++ 98. There are really two separate intentions that are not very well expressed: 1) that if you compile a C-structure declaration in C ++, then you will get the equivalent of what you had on C. 2) POD will ever be needed / use static (not dynamic).

C ++ 0x / 11 completely cancels the designation of "POD" (almost), in favor of the "trivial" and "standard layout". The standard layout is designed to capture the first intention - to create something with a layout just as you would get in C. Trivial is designed to capture support for static initialization.

Since new T vs. new T() deals with initialization, you probably want is_trivial .

I'm not sure if compiler magic is required. My immediate reaction would probably be yes, but knowing some of the things that people did with TMP, it’s hard for me to understand that someone couldn’t do this either ...

Edit: for example, perhaps the best examples are from the N3290:

 struct N { // neither trivial nor standard-layout int i; int j; virtual ~N(); }; struct T { // trivial but not standard-layout int i; private: int j; }; struct SL { // standard-layout but not trivial int i; int j; ~SL(); }; struct POD { // both trivial and standard-layout int i; int j; }; 

As you can no doubt assume, POD also a POD structure.

+53
Jun 27 '11 at 17:39
source share

For POD types, new T() is the initialization of values ​​(it will initialize the value of all members), and new T will not initialize elements (default initialization). For differences between the various forms of initialization, see this question . Bottom line: you need is_pod .

+7
Jun 27. '11 at 17:28
source share



All Articles