Skip to content

Commit

Permalink
Modify rule S6226: add diff view and documentation link
Browse files Browse the repository at this point in the history
  • Loading branch information
amelie-renard-sonarsource committed Oct 5, 2023
1 parent dfa7f01 commit a5ce9d9
Showing 1 changed file with 10 additions and 6 deletions.
16 changes: 10 additions & 6 deletions rules/S6226/cfamily/rule.adoc
Original file line number Diff line number Diff line change
@@ -1,23 +1,23 @@
== 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].


=== Noncompliant code example

[source,cpp]
[source,cpp,diff-id=1,diff-type=noncompliant]
----
struct Empty {};
struct Wrapped {
Expand All @@ -36,7 +36,7 @@ class HashMap {

=== Compliant solution

[source,cpp]
[source,cpp,diff-id=1,diff-type=compliant]
----
struct Empty {};
struct Wrapped {
Expand All @@ -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
Expand Down

0 comments on commit a5ce9d9

Please sign in to comment.