问题
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?).
回答1:
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.
回答2:
I think you're on the right track--if A is trivially copyable and B is derived from A and simply deletes some regular methods (or operators), B will be trivially copyable too.
来源:https://stackoverflow.com/questions/25830995/can-c-classes-with-deleted-methods-be-trivially-copyable