107 lines
3.8 KiB
Plaintext
107 lines
3.8 KiB
Plaintext
== 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:
|
||
|
||
|
||
____
|
||
|
||
. The behavior of a {cpp} program is undefined if it adds declarations or definitions to namespace std or to a namespace within namespace std unless otherwise specified. A program may add a template specialization for any standard library template to namespace std only if the declaration depends on a user-defined type and the specialization meets the standard library requirements for the original template and is not explicitly prohibited.
|
||
. The behavior of a {cpp} program is undefined if it declares:
|
||
** an explicit specialization of any member function of a standard library class template, or
|
||
** an explicit specialization of any member function template of a standard library class or class template, or
|
||
** 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:
|
||
|
||
____
|
||
|
||
. 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.
|
||
|
||
|
||
This rule raises an issue for any modification of the standard ``++std++`` and ``++posix++`` namespaces.
|
||
|
||
|
||
=== Noncompliant code example
|
||
|
||
[source,cpp]
|
||
----
|
||
namespace MyNamespace {
|
||
class MyType {/*...*/};
|
||
}
|
||
namespace std { // Noncompliant
|
||
int x;
|
||
void swap(MyNamespace::MyType &m1, MyNamespace::MyType &m2);
|
||
}
|
||
----
|
||
|
||
|
||
=== Compliant solution
|
||
|
||
[source,cpp]
|
||
----
|
||
namespace expanded_std {
|
||
int x;
|
||
}
|
||
namespace MyNamespace {
|
||
class MyType {/*...*/};
|
||
void swap(MyType &m1, MyType &m2);
|
||
}
|
||
----
|
||
|
||
|
||
=== 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
|
||
|
||
|
||
ifdef::env-github,rspecator-view[]
|
||
|
||
'''
|
||
== Implementation Specification
|
||
(visible only on this page)
|
||
|
||
=== Message
|
||
|
||
Choose a different namespace for this declaration.
|
||
|
||
|
||
=== Highlighting
|
||
|
||
offending namespace
|
||
|
||
|
||
'''
|
||
== Comments And Links
|
||
(visible only on this page)
|
||
|
||
=== is related to: S5963
|
||
|
||
=== on 19 Feb 2016, 22:56:07 Evgeny Mandrikov wrote:
|
||
\[~ann.campbell.2] I'm wondering why this is not marked as implemented - see \https://dory.sonarsource.com/coding_rules#rule_key=cpp%3AS3470 ?
|
||
|
||
=== on 22 Feb 2016, 16:31:34 Ann Campbell wrote:
|
||
Because that check runs against nemo [~evgeny.mandrikov]
|
||
|
||
=== on 22 Feb 2016, 17:07:48 Evgeny Mandrikov wrote:
|
||
\[~ann.campbell.2] sorry for the wrong link, but we deploy on Nemo at the same time as on Dory - \https://nemo.sonarqube.org/coding_rules#rule_key=cpp%3AS3470
|
||
|
||
=== on 22 Feb 2016, 17:30:30 Ann Campbell wrote:
|
||
Okay [~evgeny.mandrikov], this one will have the same explanation as the other one. And the same fix.
|
||
|
||
=== on 27 Aug 2020, 14:51:38 Ann Campbell wrote:
|
||
Picking on you [~amelie.renard] since you were the last one to modify this. Properly formatted, this wouldn't have additional description _after_ the code samples. All that should come before the Noncompliant example
|
||
|
||
=== on 27 Aug 2020, 14:55:45 Amélie Renard wrote:
|
||
Thanks [~ann.campbell.2], I'll change that
|
||
|
||
endif::env-github,rspecator-view[]
|