You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Warns on modifications of the ``std`` or ``posix`` namespaces which can
7
+
result in undefined behavior.
8
+
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 [bugprone-dont-modify-std-namespace]
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
+
References
59
+
----------
60
+
61
+
This check corresponds to the CERT C++ Coding Standard rule
Copy file name to clipboardExpand all lines: clang-tools-extra/docs/clang-tidy/checks/cert/dcl58-cpp.rst
+3-51Lines changed: 3 additions & 51 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -3,57 +3,9 @@
3
3
cert-dcl58-cpp
4
4
==============
5
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
0 commit comments