Apply more suggestions

This commit is contained in:
Fred Tingaud 2024-06-13 11:47:30 +02:00
parent d81c63a6fc
commit e3e0157dcc

View File

@ -12,6 +12,21 @@ Classes that avoid directly handling resources don't need to define any of the s
Omitting all of these functions from a class is known as the Rule of Zero because no special function should be defined. This rule should apply to the vast majority of classes.
[source,cpp]
----
// Compliant: vector and unique_ptr handle the resources for us
// we don't need to do any direct resource management
class RuleOfZero {
public:
void useResource();
void addValue(Value const& value);
Value getValueAtIndex(int index);
private:
std::unique_ptr<Resource> resource = std::make_unique<Resource>();
std::vector<Value> values;
};
----
The remaining classes that cannot use the Rule of Zero should be dedicated to managing a specific kind of resource and should follow a few logical rules:
* Copy operations only make sense when the corresponding move operations are available. That is because move operations are optimized copy operations allowed to steal resources from the source (the source is an r-value). At worst, copying is a valid implementation of move operations.
@ -39,7 +54,7 @@ If at least one special function needs to be customized, then:
* The copy assignment needs to be either deleted or customized.
* If you can optimize the move construction, compared to the copy, you need to provide a custom move constructor. Otherwise, you should just omit the move constructor.
* If you can optimize the move construction, compared to the copy, you should provide a custom move constructor. Otherwise, you should just omit the move constructor.
* If the copy assignment is deleted, you need to delete the move assignment.
@ -138,6 +153,11 @@ public:
ifdef::env-github,rspecator-view[]
=== External coding guidelines
* {cpp} Core Guidelines - https://github.com/isocpp/CppCoreGuidelines/blob/e49158a/CppCoreGuidelines.md#c20-if-you-can-avoid-defining-default-operations-do[C.20: If you can avoid defining default operations, do]
* {cpp} Core Guidelines - https://github.com/isocpp/CppCoreGuidelines/blob/e49158a/CppCoreGuidelines.md#c22-make-default-operations-consistent[C.22: Make default operations consistent]
== Comments And Links
(visible only on this page)