Debian Codegraph @

clang-tools-extra/docs/clang-tidy/checks/cert/dcl58-cpp.rst

raw
1 .. title:: clang-tidy - cert-dcl58-cpp
2
3 cert-dcl58-cpp
4 ==============
5
6 Modification of the ``std`` or ``posix`` namespace can result in undefined
7 behavior.
8 This check warns for such modifications.
9 The ``std`` (or ``posix``) namespace is allowed to be extended with (class or
10 function) template specializations that depend on an user-defined type (a type
11 that is not defined in the standard system headers).
12
13 The check detects the following (user provided) declarations in namespace ``std`` or ``posix``:
14
15 - Anything that is not a template specialization.
16 - Explicit specializations of any standard library function template or class template, if it does not have any user-defined type as template argument.
17 - Explicit specializations of any member function of a standard library class template.
18 - Explicit specializations of any member function template of a standard library class or class template.
19 - Explicit or partial specialization of any member class template of a standard library class or class template.
20
21 Examples:
22
23 .. code-block:: c++
24
25 namespace std {
26 int x; // warning: modification of 'std' namespace can result in undefined behavior [cert-dcl58-cpp]
27 }
28
29 namespace posix::a { // warning: modification of 'posix' namespace can result in undefined behavior
30 }
31
32 template <>
33 struct ::std::hash<long> { // warning: modification of 'std' namespace can result in undefined behavior
34 unsigned long operator()(const long &K) const {
35 return K;
36 }
37 };
38
39 struct MyData { long data; };
40
41 template <>
42 struct ::std::hash<MyData> { // no warning: specialization with user-defined type
43 unsigned long operator()(const MyData &K) const {
44 return K.data;
45 }
46 };
47
48 namespace std {
49 template <>
50 void swap<bool>(bool &a, bool &b); // warning: modification of 'std' namespace can result in undefined behavior
51
52 template <>
53 bool less<void>::operator()<MyData &&, MyData &&>(MyData &&, MyData &&) const { // warning: modification of 'std' namespace can result in undefined behavior
54 return true;
55 }
56 }
57
58 This check corresponds to the CERT C++ Coding Standard rule
59 `DCL58-CPP. Do not modify the standard namespaces
60 <https://www.securecoding.cert.org/confluence/display/cplusplus/DCL58-CPP.+Do+not+modify+the+standard+namespaces>`_.