diff --git a/rules/S6226/cfamily/rule.adoc b/rules/S6226/cfamily/rule.adoc index eaeb4d174b9..389055c08c5 100644 --- a/rules/S6226/cfamily/rule.adoc +++ b/rules/S6226/cfamily/rule.adoc @@ -1,15 +1,15 @@ == Why is this an issue? -In {cpp} every independent object needs to have a unique address, which implies that its size cannot be null. Sub-objects of another object, however, do not have this constraint. Empty base class subobjects usually don't take any space in the final object, but empty member variables, by default, take some space, at least one byte. The impact of the final size of the object may be even larger, due to padding and alignment requirements. +In {cpp}, every independent object needs to have a unique address, which implies that its size cannot be null. Sub-objects of another object, however, do not have this constraint. Empty base class subobjects usually don't take any space in the final object, but empty member variables, by default, take at least one byte. The impact on the object's size may be even larger due to padding and alignment requirements. -{cpp} 20 introduces the ``\[[no_unique_address]]`` attribute. It indicates that preserving the uniqueness of address guarantee is not important for the decorated member variable, and if the variable type is empty, no storage needs to be reserved for it in the class. +{cpp}20 introduces the ``\[[no_unique_address]]`` attribute. It indicates that preserving the uniqueness of the address guarantee is not important for the decorated member variable. If the variable type is empty, no storage needs to be reserved for it in the class. -If the type is not empty, this attribute is still valid, and has no effect. This allows to place this attribute on dependant member variables in template classes, and have the exact behavior depend on the actual template parameters. +If the type is not empty, this attribute is still valid and has no effect. This allows placing this attribute on dependent member variables in template classes and having the exact behavior depend on the template parameters. -This rule raises an issue on each member of a class that has an empty or potentially empty (in case of templates) type and does not have ``\[[no_unique_address]]`` attribute. +This rule raises an issue on each member of a class that has an empty or potentially empty (in case of templates) type and does not have a ``\[[no_unique_address]]`` attribute. Note: This rule is disabled on Windows because ``\[[no_unique_address]]`` https://devblogs.microsoft.com/cppblog/msvc-cpp20-and-the-std-cpp20-switch/#c20-no_unique_address[isn't well supported by MSVC and Clang on this platform]. @@ -17,7 +17,7 @@ Note: This rule is disabled on Windows because ``\[[no_unique_address]]`` https: === Noncompliant code example -[source,cpp] +[source,cpp,diff-id=1,diff-type=noncompliant] ---- struct Empty {}; struct Wrapped { @@ -36,7 +36,7 @@ class HashMap { === Compliant solution -[source,cpp] +[source,cpp,diff-id=1,diff-type=compliant] ---- struct Empty {}; struct Wrapped { @@ -56,6 +56,10 @@ class HashMap { This rule does not apply to fields whose class has a non-default alignment. +== Resources + +* {cpp} reference - https://en.cppreference.com/w/cpp/language/attributes/no_unique_address[C++ attribute: no_unique_address] + ifdef::env-github,rspecator-view[] ''' == Comments And Links