Skip to content

Commit

Permalink
Modify rule S3470: Clarify that we allow std class specialization
Browse files Browse the repository at this point in the history
  • Loading branch information
frederic-tingaud-sonarsource authored Mar 27, 2024
1 parent bfb697d commit 29a2254
Showing 1 changed file with 7 additions and 9 deletions.
16 changes: 7 additions & 9 deletions rules/S3470/cfamily/rule.adoc
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
== Why is this an issue?

It may seem tidy to add your new declarations to the ``++std++`` or ``++posix++`` namespaces, but doing so results in undefined behavior. The {cpp}14 Standard, [namespace.std] (ISO/IEC 14882-2014 §17.6.4.2.1), paragraphs 1 and 2 states:
It may seem tidy to add your new declarations to the `std` or `posix` namespaces, but doing so results in undefined behavior. The {cpp}14 Standard, [namespace.std] (ISO/IEC 14882-2014 §17.6.4.2.1), paragraphs 1 and 2 states:


____
Expand All @@ -12,17 +12,20 @@ ____
** an explicit or partial specialization of any member class template of a standard library class or class template.
____

In addition to restricting extensions to the ``++std++`` namespace, the {cpp}14 Standard goes on in §17.6.4.2.2 to say:
In addition to restricting extensions to the `std` namespace, the {cpp}14 Standard goes on in §17.6.4.2.2 to say:

____
. The behavior of a {cpp} program is undefined if it adds declarations or definitions to namespace posix or to a namespace within namespace posix unless otherwise specified. The namespace posix is reserved for use by ISO/IEC 9945 and other POSIX standards.
____

You may think that it's legitimate to reopen ``++std++`` to define a version of extension points (``++std::swap++``, ``++std::hash++``...) that work with your types, but it's not necessary: If you call these extension points according to the correct pattern, the user-defined version will be found too.
However, the standard allows specializing standard class templates in namespace `std`. In that case, the specialization must respect the requirement of the original template and has to be for a "program-defined type" (a type that is specific to the program, by opposition to a type from the standard).

You may therefore think that it's legitimate to reopen `std` to define a version of extension points (``++std::swap++``, ``++std::hash++``...) that work with your types, but it's not necessary: If you call these extension points according to the correct pattern, the user-defined version will be found too.

This rule raises an issue for any modification of the standard ``++std++`` and ``++posix++`` namespaces.
The only extension points for which the specialization is the recommended approach are ``++std::out_ptr++`` and ``++std::inout_ptr++``.

This rule raises an issue for any modification of the standard `std` and `posix` namespaces that is not a template specialization.


=== Noncompliant code example
Expand Down Expand Up @@ -53,11 +56,6 @@ namespace MyNamespace {
----


=== Exceptions

A namespace fragment that only contains template specializations or explicit instantiations is ignored by this rule.


== Resources

* https://wiki.sei.cmu.edu/confluence/x/Xnw-BQ[CERT, DCL58-CPP.] - Do not modify the standard namespaces
Expand Down

0 comments on commit 29a2254

Please sign in to comment.