Discussion: If a class is a resource handle, it needs a constructor, a destructor, and copy and/or move operations
讨论:如果一个类是资源句柄,则它需要一个构造函数,一个析构函数以及复制和/或移动操作
Reason(原因)
To provide complete control of the lifetime of the resource. To provide a coherent set of operations on the resource.
提供对资源生命周期的完全控制。在资源上提供连贯的操作集。
Example(示例)
代码语言:javascript复制??? Messing with pointers
Note(注意)
If all members are resource handles, rely on the default special operations where possible.
如果所有成员都是资源句柄,请尽可能依靠默认的特殊操作。
代码语言:javascript复制template<typename T> struct Named {
string name;
T value;
};
Now Named has a default constructor, a destructor, and efficient copy and move operations, provided T has.
现在,Named类具有默认的构造函数,析构函数以及有效的复制和移动操作(如果T具有)。
Enforcement(实施建议)
In general, a tool cannot know if a class is a resource handle. However, if a class has some of the default operations, it should have all, and if a class has a member that is a resource handle, it should be considered as resource handle.
通常,工具无法知道类是否是资源句柄。但是,如果类具有某些默认操作,则应具有全部默认操作,并且如果类具有作为资源句柄的成员,则应将其视为资源句柄。
原文链接https://github.com/isocpp/CppCoreGuidelines/blob/master/CppCoreGuidelines.md#discussion-if-a-class-is-a-container-give-it-an-initializer-list-constructor