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

前端 未结 3 979
终归单人心
终归单人心 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条回答
  •  萌比男神i
    2020-12-06 16:39

    Potatoswatter found an error in my conclusions. C++ explicitly allows an implementation to explicitly define an assignment operator "with the same apparent semantics". This will make it a non-trivially copyable type. Making it community wiki...


    It seems to me you don't want to test against PODnes, but against trivially copyable, which is way less restricting. Because that is how C++0x constraints types that can be used with memcpy and friends.

    And while I don't think there are any guarantees about PODness of std::array, there are guarantees about trivial copyability, as the following shows (if I haven't got an error in the conclusions). As we know std::array is an aggregate, and aggregates are

    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).

    While trivially copyability is defined for a class that has class that

    • has no non-trivial copy constructors (12.8),
    • has no non-trivial move constructors (12.8),
    • has no non-trivial copy assignment operators (13.5.3, 12.8),
    • has no non-trivial move assignment operators (13.5.3, 12.8), and
    • has a trivial destructor (12.4).

    std::array has no destructor (as a comment in the definition of std::array says). This does not seem to follow from the definition of aggregate classes though, even though the comment in std::array's class definition claims that.

    The remaining 4 requirements follow from the absence of bases, virtual functions and user provided versions for those 4 special member functions for aggregates.

提交回复
热议问题