Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Can C++ classes with deleted methods be trivially copyable?

I want class B to inherit all but a few methods of class A (which is assumed to be trivially copyable), and still be trivially copyable. In C++11 I can delete methods. Take for example:

class A { // trivially copyable
   // private stuff here
public:
   A& operator += (const A&);
   // other public stuff here
};

class B: public A {
public:
   B& operator += (const A&) = delete;
};

Is B trivially copyable? I know there are issues regarding the deletion of special methods, but the compound assignment is not a special method (right?).

like image 819
FreeQuark Avatar asked Dec 15 '22 20:12

FreeQuark


1 Answers

Yes, B is trivially copyable - regardless of what you do to non-special member functions.

N3337, §9/6:

A trivially copyable class is a 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).

but the compound assignment is not a special method (right?)

No, it's not.

N3337, §12/1:

The default constructor (12.1), copy constructor and copy assignment operator (12.8), move constructor and move assignment operator (12.8), and destructor (12.4) are special member functions.

like image 158
Columbo Avatar answered Jan 23 '23 15:01

Columbo