From e3e0157dcc691787e29ef1a9ccac8caa84c46b3e Mon Sep 17 00:00:00 2001 From: Fred Tingaud Date: Thu, 13 Jun 2024 11:47:30 +0200 Subject: [PATCH] Apply more suggestions --- rules/S3624/cfamily/rule.adoc | 22 +++++++++++++++++++++- 1 file changed, 21 insertions(+), 1 deletion(-) diff --git a/rules/S3624/cfamily/rule.adoc b/rules/S3624/cfamily/rule.adoc index 8c48cd5ddb..066bb257bb 100644 --- a/rules/S3624/cfamily/rule.adoc +++ b/rules/S3624/cfamily/rule.adoc @@ -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 = std::make_unique(); + std::vector 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)