Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why is a struct with a deleted copy constructor not a POD type?

We have the following method to test if our structure is a POD or not. It always returns true:

bool podTest() {
  struct podTest {
    int count;
    int x[];
  };

  return std::is_pod<podTest>::value;  //Always returns true
}

So far so good. Now we make one change and delete the copy constructor:

bool podTest() {
  struct podTest {
    podTest(const podTest&) = delete;
    int count;
    int x[];
  };

  return std::is_pod<podTest>::value;  //Always returns false
}

This always returns false. After reading over the definition of is_pod I am still struggling to understand what requirement it violates. What am I missing?

This is being compiled on godbolt using gcc 6.1, with -std=c++14

like image 559
Chuu Avatar asked Feb 06 '17 23:02

Chuu


People also ask

What is deleted copy constructor?

The copy constructor and copy-assignment operator are public but deleted. It is a compile-time error to define or call a deleted function.

Why do we delete the copy constructor?

When to delete copy constructor and assignment operator? Copy constructor (and assignment) should be defined when ever the implicitly generated one violates any class invariant. It should be defined as deleted when it cannot be written in a way that wouldn't have undesirable or surprising behaviour.

Does struct have copy constructor?

How to Define Copy Constructor for Struct in C++ You can define a copy constructor for a struct in C++ by implementing a special member function. The latter function usually will accept the reference to the struct object and returns the struct by value.

What happens if we remove and operator in case of copy constructor?

When we don't provide an implementation of copy constructor (and assignment operator) and try to initialize an object with the already initialized object of the same class then copy constructor gets called and copies members of class one by one in the target object.

What happens if there are no copy constructors for a class?

If no user-defined copy constructors are provided for a class type (struct, class, or union), the compiler will always declare a copy constructor as a non-explicitinline publicmember of its class. This implicitly-declared copy constructor has the form T::T(constT&)if all of the following are true:

What is deleted copy constructor in C++?

Deleted Copy Constructor is used if you are Avoiding implicit generation of the copy constructor. In example, copy constructor can be defined with attributes as delete; This is a full example with a Deleted Copy Constructor, here we put reminder where compile error and avoids to generate a new class by using copy constructor,

What is a non explicit copy constructor?

Implicitly-declared copy constructor. If no user-defined copy constructors are provided for a class type (struct, class, or union), the compiler will always declare a copy constructor as a non-explicit inline public member of its class.

Is noncopyable a true pod type?

Explicitly defaulted special member functions are still considered trivial, so there is no performance penalty, and noncopyable is not prevented from being a true POD type. The copy constructor and copy-assignment operator are public but deleted.


2 Answers

Aha!

From [class]:

A POD struct is a non-union class that is both a trivial class and a standard-layout class

where a trivial class is:

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

But in [class.copy]:

If there is no user-declared constructor for class X, a non-explicit constructor having no parameters is implicitly declared as defaulted (8.4).

Your podTest, when you explicitly deleted the copy constructor, has no default constructor. So it's not a trivial class, so it's not a POD. If you add in:

podTest() = default;

Then it'll become a POD again.

like image 56
Barry Avatar answered Oct 31 '22 00:10

Barry


Because deleted copy constructors are allowed for POD types only after C++14. I would assume, you are compiling your code in C++11 mode.

like image 36
SergeyA Avatar answered Oct 31 '22 00:10

SergeyA