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

时光毁灭记忆、已成空白 提交于 2020-01-24 05:47:46

问题


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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!