0

Is an array in C++ always a POD?

We know that an array is always an aggregate, regardless of whether the underlying type is an aggregate.

(See here)

But is an array always a POD, regardless of whether its elements are non-POD?

Gert Arnold
  • 105,341
  • 31
  • 202
  • 291
Michele Piccolini
  • 2,634
  • 16
  • 29
  • I can't tell from the answers [here](https://stackoverflow.com/questions/146452/what-are-pod-types-in-c), that's why I'm asking. – Michele Piccolini Feb 08 '22 at 13:46
  • 1
    Unfortunate that the top answer for that duplicate incorrectly states that a POD type must be a class. – Drew Dormann Feb 08 '22 at 13:48
  • Exactly @DrewDormann, that's an incredibly low-detail and incomplete answer. – Michele Piccolini Feb 08 '22 at 13:50
  • 2
    I think I answered myself (but I still would like someone to confirm this). From the ISO C++03: "Scalar types, POD-struct types, POD-union types (clause 9), **arrays of such types** and cv-qualified versions of these types (3.9.3) are collectively called POD types." I interpret this as "arrays of PODs are PODs, but not all arrays". Is this correct? – Michele Piccolini Feb 08 '22 at 13:51
  • I suggest you follow the link in that answer, and look at the very detailed explanation of the changes made for C++11 in regards to POD types. – Some programmer dude Feb 08 '22 at 13:54
  • And if you want a language-lawyer explanation then please add that tag. If you want explanation for a specific version of the C++ specification, then add such a tag as well. – Some programmer dude Feb 08 '22 at 13:54
  • Thanks @Someprogrammerdude, I added the tags. Didn't know there was a language-lawyer tag! – Michele Piccolini Feb 08 '22 at 13:56
  • Yeah @DrewDormann, from the top answer I again read "POD-classes, POD-unions, scalar types, and **arrays of such types** are collectively called POD-types.", so I suspect that it's the case that not all arrays are PODs (while all arrays are aggregates). Though, it's a specific information that is hard to find (unless one goes through the whole explanation), there are no specific examples regarding this (all POD examples that I found so far are with structs, even if a POD is not necessarily a struct), and if someone more experienced than me could confirm this, I think it would be useful! – Michele Piccolini Feb 08 '22 at 14:01
  • 1
    https://stackoverflow.com/a/7189821/1387438 this answer has section about PODs which nicely explains everything. It says that rule applies recursively. – Marek R Feb 08 '22 at 14:01
  • 1
    "POD type" isn't really used by the standard anymore in C++20. It is used only for a deprecated type trait. – user17732522 Feb 08 '22 at 14:15
  • Ok @user17732522. Is it a concept not worth learning? What other concepts would you suggest learning instead? (And are they best learnt by referring to concepts like PODs and aggregates, or are these just abandoned and might create just confusion to someone that approaches these topics for the first time?) – Michele Piccolini Feb 08 '22 at 14:23
  • 1
    @MichelePiccolini The "aggregate" concept is still important, but "POD" has been split into "trivial"/"trivally copyable"/"trivially destructible" and "standard-layout" since C++11 as explained in the linked duplicate. "POD" is still used informally, but if you want to talk about precise meaning in the language, you would use these newer concepts instead. – user17732522 Feb 08 '22 at 14:26
  • Thanks a lot @user17732522 ! I'll read up on those. – Michele Piccolini Feb 08 '22 at 14:44

0 Answers0