Is std::array guaranteed to be POD if T is POD?

前端 未结 3 975
终归单人心
终归单人心 2020-12-06 16:27

I\'m currently writing a C++ memory editing library and for the read/write APIs I use type traits (std::is_pod, std::is_same) and boost::enable_if to provide 3 overloads:

3条回答
  •  挽巷
    挽巷 (楼主)
    2020-12-06 16:42

    §23.3.1:

    An array is an aggregate (8.5.1) that can be initialized with the syntax array a = { initializer-list }; where initializer-list is a comma separated list of up to N elements whose types are convertible to T.

    In C++03, POD was defined in terms of aggregate: a class where every subobject is native or an aggregate is POD. So, by backwards compatibility, a C++0x std::array is POD.

    Or, to be anal, one can compare the bullet-points of §9/5 (defining trivial class) 9/6 (defining standard-layout) and 9/9 (combining preceding requirements into POD) with those of 8.5.1/1, which defines aggregates.

    8.5.1:

    An aggregate is an array or a class (Clause 9) with no user-provided constructors (12.1), no brace-or-equal- initializers for non-static data members (9.2), no private or protected non-static data members (Clause 11), no base classes (Clause 10), and no virtual functions (10.3).

    Indeed the requirements in Clause 9 cover array as long as its element type is also POD and the implementation does not declare operator= or move inside array in addition to the specifications.

    To be really anal, 17.5.2.2 says

    1. For the sake of exposition, Clauses 18 through 30 and Annex D do not describe copy/move constructors, assignment operators, or (non-virtual) destructors with the same apparent semantics as those that can be generated by default (12.1, 12.4, 12.8).
    2. It is unspecified whether the implementation provides explicit definitions for such member function signa- tures, or for virtual destructors that can be generated by default.

    The note in the pseudo-code for template class array is

    // No explicit construct/copy/destroy for aggregate type

    Does construct/copy/destroy include operator= (assignment) or move? It probably should, but I don't think, by the strictest reading, it does.

    Note that this "affects" not only POD-ness, but also trivial copyability as Johannes mentions.

提交回复
热议问题