Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

trivial vs. standard layout vs. POD

Tags:

c++

typetraits

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

Specifically, I want to determine whether new T is different from new T() for any template parameter T. Which of the type traits is_trivial, is_standard_layout and is_pod should I choose?

(As a side question, can any of these type traits be implemented without compiler magic?)

like image 854
fredoverflow Avatar asked Jun 27 '11 17:06

fredoverflow


People also ask

Why is_ POD deprecated?

“POD” is equivalent to “trivial and standard layout”, but for many code patterns, a narrower restriction to just “trivial” or just “standard layout” is appropriate; to encourage such precision, the notion of “POD” was therefore deprecated.

What does standard layout mean?

Standard-layout types can have user-defined special member functions. In addition, standard layout types have these characteristics: no virtual functions or virtual base classes. all non-static data members have the same access control. all non-static members of class type are standard-layout.

What is trivial type?

A trivial type is a type whose storage is contiguous (trivially copyable) and which only supports static default initialization (trivially default constructible), either cv-qualified or not. It includes scalar types, trivial classes and arrays of any such types.

What is a POD C++?

POD is an acronym in C++ that means plain old data. It is a class/struct that ONLY has member variables and no methods, constructors, destructors, virtual functions, etc.


1 Answers

I don't think it can be done in truly layman's terms, at least without a lot of extra explanation. One important point is static vs. dynamic initialization, but explaining that to a layman would be several pages in itself...

PODs were (mis-)defined in C++98. There are really two separate intents involved, neither expressed very well: 1) that if you compile a C struct declaration in C++, what you get should be equivalent to what you had in C. 2) A POD will only ever need/use static (not dynamic) initialization.

C++0x/11 drops the "POD" designation (almost) entirely, in favor of "trivial" and "standard layout". Standard layout is intended to capture the first intent -- creating something with a layout the same as you'd get in C. Trivial is intended to capture the support for static initialization.

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

I'm not sure about compiler magic being required. My immediate reaction would be probably yes, but knowing some of the things people have done with TMP, I have a hard time being certain somebody couldn't do this too...

Edit: for examples, perhaps it's best to just quote the examples from 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 undoubtedly guess, POD is also a POD struct.

like image 93
Jerry Coffin Avatar answered Sep 22 '22 04:09

Jerry Coffin