Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

c++: May POD types contain const non-pointer members?

Tags:

c++

standards

#include <iostream>

struct A {
  const int test_;
};

static_assert(std::is_pod<A>::value, "must be POD type");

int main()
{
    std::cout<<"Hello World";
    return 0;
}

On Clang and GCC std::is_pod<A>::value is true, while on ICC and MSVC it is false.

If const int test_; is replaced with either int test_; or const int* test_ then it also passes on ICC and MSVC.

What does the standard say?

like image 360
matthias_buehlmann Avatar asked Jun 08 '21 18:06

matthias_buehlmann


People also ask

What is non POD type?

A POD (plain old data) object has one of these data types--a fundamental type, pointer, union, struct, array, or class--with no constructor. Conversely, a non-POD object is one for which a constructor exists.

Is a struct a POD?

The struct MyStruct has no user-defined ctor, dtor, etc and hence is a POD.

Can POD have a constructor?

POD's and trivial classes are now allowed to have constructors, as long as trivial default constructor, copy constructor, copy assignment, and destructor are available. POD's and standard-layout types are now allowed to have access control. All non-static data members must have the same access control.

What is POD programming?

In computer science and object-oriented programming, a passive data structure (PDS, also termed a plain old data structure, or plain old data, POD) is a term for a record, to contrast with objects.


Video Answer


2 Answers

N4659

12/10

A POD struct is a non-union class that is both a trivial class and a standard-layout class, and has no non-static data members of type non-POD struct, non-POD union (or array of such types).

For trivial

12/6

A trivial class is a class that is trivially copyable and has one or more default constructors

There is no available default constructor.

Further, for trivially copyable, the requirements include

12/6.2

that has at least one non-deleted copy constructor, move constructor, copy assignment operator, or move assignment operator

Since you include a const int, it's not trivially copyable (no assignment operators), so it is not a pod

^ I was wrong, it is copyable by having the constructors. I misread the "or" as "and"

like image 90
Ryan Haining Avatar answered Oct 06 '22 20:10

Ryan Haining


One of the requirements for POD type is to be a trivial type.

[class]
10 A POD struct 109 is a non-union class that is both a trivial class and a standard-layout class, and has no non-static data members of type non-POD struct, non-POD union (or array of such types). where trivial type

while trivial is defined in the same section as

6 A trivial class is a class that is trivially copyable and has one or more default constructors (15.1), all of which are either trivial or deleted and at least one of which is not deleted.

Since A has a field with const-qualifier which requires mandatory initialization, its implicitly declared default constructor is deleted. Therefore A is not trivial type and not a POD type. So VS and ICC are correct here. But it is probably not a big deal since POD trait became deprecated in C++20 and should be avoided in code using preceding standards.

like image 22
user7860670 Avatar answered Oct 06 '22 20:10

user7860670